http:// localhost:57501 / api / addDatabase上的Web服務具有以下代碼。 [System.Web.Mvc.HttpPost]
public ActionResult Post(addDatabase pNuevaConeccion)
{
pNuevaConeccion.insertarMetaData();
return null;
}Ajax函數(shù)位于javascript上,該javascript根據(jù)http:// localhost:1161 / CreateServer上的給定值創(chuàng)建JSON 。$(document).ready(function(){$("#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 }
}
});});}); 當我運行此代碼時會彈出一個提示“未定義”但如果我刪除了contentType,則警報不會顯示。問題是函數(shù)Post(來自Web服務)接收的變量是NULL,即使我知道名為dataa的JSON不是NULL,因為我做了一個console.log。我看過各種各樣的例子,幾乎所有的例子都說我應該使用相對URL,但問題是因為有2個不同的域,當我嘗試它時,它找不到URL,因為它不在同一個localhost中。
使用jQuery post JSON接收null的Web服務
開滿天機
2019-04-24 15:19:31