javascript jQuery.ajax“数据”参数语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7258933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:29:03  来源:igfitidea点击:

jQuery.ajax "data" parameter syntax

c#javascriptjqueryasp.netajax

提问by Seraph812

I am trying to pass the contents of a javascript variable to the server for processing. I can pass static strings no problem but when I pass a variable containing a string, the WebMethod is not called. Here is my code: (Client)

我正在尝试将 javascript 变量的内容传递给服务器进行处理。我可以传递静态字符串没问题,但是当我传递包含字符串的变量时,不会调用 WebMethod。这是我的代码:(客户端)

function expand(checkbox) 
{
    var selectedrow = checkbox.parentNode.parentNode;
    var rowindex = selectedrow.rowIndex;
    var parent = document.getElementById("parentTable");
    var NextRow = parent.rows[rowindex + 1];

    var cols = selectedrow.cells[1];
    var ID = cols.firstElementChild.attributes.value;


    $.ajax({
        type: "post",
        url: "Playground.aspx/childBind",
        data: "{sendData: ID}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) { alert("successful!" + result.d); }
    })

    NextRow.style.visibility = "visible";
}

(Server)

(服务器)

[WebMethod]
    public static string childBind(string sendData)
    {
        return String.Format("Hello");
    }

Now, if I were to try data: "{sendData: "ok"}", the WebMethod gets called and returns a response. How is my syntax wrong?

现在,如果我要尝试 data: "{sendData: "ok"}",WebMethod 将被调用并返回响应。我的语法怎么错了?

回答by ShankarSangoli

You don't have to pass it as a string. Since IDis a javascript variable you have to pass its value. When you pass data as "{sendData: ID}"it will not pass the value of ID.

您不必将其作为字符串传递。由于ID是 javascript 变量,因此您必须传递其值。当您传递数据时,因为"{sendData: ID}"它不会传递ID.

Try this

试试这个

data: { sendData: ID }

回答by Pierre-Luc Champigny

You were sending a string instead of an object ("{sendData: ID}"instead of {sendData: ID}). And the data you were sending wasn't JSON. So remove the contentType line and change the data line. You should re-write this as:

您发送的是字符串而不是对象("{sendData: ID}"而不是{sendData: ID})。您发送的数据不是 JSON。所以删除 contentType 行并更改数据行。您应该将其重写为:

$.ajax({
    type: "post",
    url: "Playground.aspx/childBind",
    data: {sendData: ID},
    dataType: "json",
    success: function (result) { alert("successful!" + result.d); }
})

You can also write this, if you want to send JSON:

如果你想发送 JSON,你也可以这样写:

$.ajax({
    type: "post",
    url: "Playground.aspx/childBind",
    data: $.getJSON({sendData: ID}),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (result) { alert("successful!" + result.d); }
})

回答by Kris Krause

Since you are using jQuery run these tests for the answer:

由于您使用的是 jQuery,请运行这些测试以获得答案:

var ID = 45;

var x = "{sendData: ID}";

alert(jQuery.isPlainObject(x));  // false

var y = { sendData: ID};

alert(jQuery.isPlainObject(y)); // true

Here is the jsFiddle:

这是jsFiddle:

http://jsfiddle.net/3ugKE/

http://jsfiddle.net/3ugKE/

回答by totallyNotLizards

You are passing in 'ID' as a stringrather than a variable. All you need to do is remove the quotes around your data object.

您将“ID”作为字符串而不是变量传入。您需要做的就是删除数据对象周围的引号。

Further reading on JSON and javascript objects:

进一步阅读 JSON 和 javascript 对象:

http://www.json.org/

http://www.json.org/

http://www.w3schools.com/js/js_objects.asp

http://www.w3schools.com/js/js_objects.asp

回答by HamidReza

you can use this code :

您可以使用此代码:

$.ajax({
        type: "post",
        url: "Playground.aspx/childBind",
        data: JSON.stringify({sendData: ID}),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) { alert("successful!" + result.d); }
    })