【问题标题】:AJAX call with JSON array not getting triggered to server side带有 JSON 数组的 AJAX 调用未触发到服务器端
【发布时间】:2014-09-16 17:29:49
【问题描述】:

我正在尝试使用 jquery ajax 将 json 数组传递给我的代码。

什么有效

如果我使用带有空 json 字符串 ("{}") 的 jQuery .ajax() 调用我的代码隐藏函数,我的代码隐藏函数将毫无问题地触发。

我想做的事

如果我将一个 Json 字符串放到 jQuery .ajax() 的“数据”属性中,然后使用字符串参数创建一个代码隐藏函数,它就不会再被触发。

这是我的代码:

客户端

function SendAjax() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Default.aspx/MyFunction",
        data: '{"foo":"bar"}',
        dataType: "json",
        success: function (msg) {
           //DO Somtething
        },
        error: function (xhr, status, error) {
            //DO Somtething
        }
    });
}

服务器端

[WebMethod]
public static string MyFunction(string jsonData)
{   
   //Do something
   return "test";
}
  • 我的 json 数组格式正确
  • 我有一个静态函数
  • 我有[WebMethod]

我错过了什么吗?

【问题讨论】:

标签: c# asp.net ajax json


【解决方案1】:

试试这个 100% 的工作

function SendAjax() {
            var city = "ABC";

            $.ajax({
                type: "POST",
                url: "Default4.aspx/MyFunction",
                data: "{jsonData:" + JSON.stringify(city) + "}",
                contentType: "application/json; charset=utf-8",

                dataType: "json",
                success: function (msg) {
                   alert(msg.d);
                },
                error: function (xhr, status, error) {
                    //DO Somtething
                }
            });
        }




[WebMethod]
    public static string MyFunction(string jsonData)
    {
        //Do something
        return "test";
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    相关资源
    最近更新 更多