2016-04-05 13 views
0

http://localhost:57501/api/addDatabase web hizmeti aşağıdaki koda sahiptir.jQuery gönderimi ile boş Web hizmeti alımı JSON

[System.Web.Mvc.HttpPost] 
    public ActionResult Post(addDatabase pNuevaConeccion) 
    { 
     pNuevaConeccion.insertarMetaData(); 
     return null; 
    } 

Ajax fonksiyonu http://localhost:1161/CreateServer üzerinde vermek değerlerinden JSON yaratan bir javascript üzerindedir.

$ (belge) .ready (fonksiyonu() {

$("#createServer").click(function (e) { 

    e.preventDefault(); //Prevent the normal submission action 

    var frm = $("#CreateServerID"); 
    var dataa = JSON.stringify(frm.serializeJSON()); 
    console.log(dataa); 

    $.ajax({ 

     type: 'POST', 
     url: 'http://localhost:57501/api/addDatabase/', 
     contentType: 'application/json; charset=utf-8', 
     crossDomain: true, 
     //ContentLength: dataa.length, 
     data: dataa, 
     datatype: 'json', 
     error: function (response) 
     { 
      alert(response.responseText); 

     }, 
     success: function (response) 
     { 
      alert(response); 
      if (response == "Database successfully connected") { 
       var pagina = "/CreateServer" 
       location.href = pagina 


      } 
     } 
    }); 

}); 

}); Bu kodu çalıştırdığımda "undefined" ("undefined") belirten bir uyarı açılır, ancak contentType öğesini silersem uyarı görünmez. Sorun, Post (Web hizmetinden) işlevinin aldığı değişkenlerin, bir konsol.log yaptığımdan beri JSON adlı DataA'nın NULL olmadığını bilmeme rağmen NULL olduğu.

Çeşitli örnekler gördüm ve hemen hemen hepsi göreli bir URL kullanmam gerektiğini söylüyor ama sorun şu ki 2 farklı etki alanı bulunduğundan ve denediğimde URL’yi bulamadı çünkü URL’yi bulamadı aynı localhost'ta.

cevap

1

Web hizmeti, null yerine bir JSON biçimini döndürmelidir. örneğin aşağıdaki gibi.

public JsonResult Post() 
    { 
     string output = pNuevaConeccion.insertarMetaData(); 

     return Json(output, JsonRequestBehavior.AllowGet); 
    } 
0

deneyin web yöntemini çağırarak için bu kodu kullanmayı

$.ajax({ 
     method: "POST", 
     contentType: "application/json; charset=utf-8", 
     data: dataa, 
     url: 'http://localhost:57501/api/addDatabase/', 
     success: function (data) { 
      console.log(data);    
     }, 
     error: function (error) { 
      console.log(error); 
     } 
}); 
0
its my old code.(ensure action parameter variable name and post variable name are same) 

$('#ConnectionAddres_ZonesId').change(function() { 
      var optionSelected = $(this).find("option:selected"); 
      var id = { id: optionSelected.val() }; 

      $.ajax({ 
       type: "POST", 
       url: '@Url.Action("GetParetArea", "Customers")', 
       contentType: "application/json;charset=utf-8", 
       data: JSON.stringify(id), 
       dataType: "json", 
       success: function (data) { 
        $('#ConnectionAddres_ParentAreaId').empty().append('<option value="">Select parent area</option>'); 
        $.each(data, function (index, value) { 
         $('#ConnectionAddres_ParentAreaId').append($('<option />', { 
          value: value.Id, 
          text: value.Area 
         })); 
        }); 
       }, 
      }); 
     }); 

public ActionResult GetParetArea(int id) 
     { 
      var parents=""; 
      return Json(parents, JsonRequestBehavior.AllowGet); 
     }