簡體   English   中英

Web服務使用jQuery post JSON接收null

[英]Web service receiving null with jQuery post JSON

http:// localhost:57501 / api / addDatabase上的Web服務具有以下代碼。

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

Ajax函數位於從http:// localhost:1161 / CreateServer上的Give值創建JSON的javascript上。

$(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,則不會顯示警報。 問題是,即使我知道名為dataa的JSON由於執行console.log也不為NULL,所以函數Post(從Web服務)接收的變量為NULL。

我看過各種示例,幾乎所有的示例都說我應該使用相對URL,但是問題在於,由於存在2個不同的域,並且當我嘗試使用它時,由於它不在同一本地主機中,因此找不到URL 。

Web服務應返回JSON格式,而不是null。 像下面的例子。

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

        return Json(output, JsonRequestBehavior.AllowGet);  
    }  

嘗試使用此代碼來調用Web方法

$.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);  
        }
});
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);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM