代码之家  ›  专栏  ›  技术社区  ›  Hannoun Yassir

将JSON发送到webmethod?

  •  7
  • Hannoun Yassir  · 技术社区  · 16 年前

    如何使用jQuery将JSON对象发送到webmethod?

    7 回复  |  直到 16 年前
        1
  •  21
  •   TheVillageIdiot    16 年前

    请参阅 this

    编辑:-Dave正在调用没有任何参数的方法,您可以替换为空 数据 属性,其中包含要发送的实际数据:

    $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: "{'name':'tiger1','hobbies':['reading','music']}",//PUT DATA HERE
      contentType: "application/json; charset=utf-8",
      dataType: "json",
    
        2
  •  11
  •   Christian C. Salvadó    16 年前

    WebMethods需要一个包含JSON的字符串,该字符串将在服务器端解析,我使用 JSON.stringify 参数 对象转换为字符串,并发送数据,我有一个如下函数:

    jQuery.executePageMethod = function(location, methodName, methodArguments,
                                        onSuccess, onFail) {
        this.ajax({
            type: "POST",
            url: location + "/" + methodName,
            data: JSON.stringify(methodArguments), // convert the arguments to string
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data, status) {
                var jsonData = JSON.parse(data.d);
                onSuccess(jsonData, status);
            },
            fail: onFail
        });
    };
    

    我建议您将 json2.js 页面中的语法分析器,以便跨浏览器提供JSON.stringify函数。

        3
  •  6
  •   ceejayoz    16 年前

    jquery-json library . 一旦包括:

    var json = $.toJSON(your_object);
    
        4
  •  5
  •   Jeff Sternal    16 年前

    我见过的最方便的解决方案通过使用 the open-source JSON2.js 用于解析和“字符串化”复杂对象数据的库。

    这两篇优秀的文章将详细介绍:

    第二篇文章可能与您特别相关,尽管它称为web 服务 具有以下签名的方法。。。

    public void SendValues(List<string> list)
    

    ... 它演示了如何使用JSON2.js库来呈现 List<string> 在javascript中(使用jQuery,此示例直接取自第二篇文章):

    var list = ["a", "b", "c", "d"];
    var jsonText = JSON.stringify({ list: list });
    
    // The 'list' is posted like this
    
    $.ajax({
        type: "POST",
        url: "WebService1.asmx/SendValues",
        data: jsonText,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function() { alert("it worked"); },
        failure: function() { alert("Uh oh"); }
    });
    

    只需使用WebMethodURL代替web服务的URL。

        5
  •  0
  •   lomaxx    16 年前

    您需要使用Ajax发布它,并在webmethod上接受传入的字符串。然后需要使用JavaScript反序列化程序将其转换为服务器端的对象。

        6
  •  0
  •   naugtur    16 年前

    JSON.stringify确实有帮助,但:

    1. 它不是跨浏览器的 请看这里: http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/#

    2. 对于浏览器内置功能-每个浏览器都会有问题。如果使用上述序列化,则需要:

      • 注意“在字符串中”
        7
  •  0
  •   Nalan Madheswaran    13 年前

    示例代码如下:

    var dataString = JSON.stringify({
                                contractName: contractName,
                                contractNumber: contractNumber
                            });
    
                            $.ajax({
                                type: "POST",
                                url: "CreateQuote.aspx/GetCallHistory",
                                data: dataString,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (result) {
                                    alert(result.CallHistoryDescription);
                                        OpenLightBox('divDelete');
    
                                }
                            });
    
    
            [System.Web.Services.WebMethod]
            public static object GetCallHistory(string contractName, string contractNumber)
            {
                return new
                {
                    CallHistoryDescription = "Nalan"
                };
    
            }
    
    推荐文章