jQuery.ajax"数据"参数语法 [英] jQuery.ajax "data" parameter syntax

查看:211
本文介绍了jQuery.ajax"数据"参数语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个javascript变量的内容传递到服务器进行处理。我可以通过静态字符串没有问题,但是当我传递一个包含字符串的变量,将WebMethod不叫。这是我的code: (客户端)

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)

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";
}

(服务器)

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

现在,如果我尝试的数据:{SendData的:确定},将WebMethod被调用和返回响应。如何为我的语法错误?

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

推荐答案

您是发送对象的字符串,而不是({送出数据:ID}而不是 {送出数据:ID} )。而你发送的数据是不是JSON。所以删除的contentType线,改变数据线。你应该重新写为:

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:

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

您还可以这样写,如果你要发送的JSON:

You can also write this, if you want to send 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); }
})

这篇关于jQuery.ajax"数据"参数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆