【问题标题】:How to send xml file data over ajax to Webmethod?如何通过ajax将xml文件数据发送到Webmethod?
【发布时间】:2020-10-12 10:32:11
【问题描述】:

我在通过 ajax 调用向我的 Web 方法(C# Webforms)发送 XML 时遇到问题:

我已经使用 JSON 测试了 ajax 调用,并且能够将 JSON 发送到 Web 方法。

我的响应状态代码返回 200,但当我尝试发送 xml 时它没有触发 webmethod 上的调试器。

谁能告诉我我做错了什么?

JavaScript:

 var test = '<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note>';
                
                $.ajax({
                    type: "POST",
                    url: "/App/drawIOjs/draw.aspx/GetDocument",
                    data: { xml: test }, 
                    contentType: "text/xml; charset=utf-8",
                    dataType: "text",
                    success: function (msg) {
                        debugger;
                        alert(msg);
                    },
                    failure: function (msg) {
                        // failure code here
                        debugger;
                        alert(msg);
                    }
                });

网络方法:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]   
    public static string GetDocument(string test   ) 
    {
         string testing = test;       

        return "This string is from Code behind";
    }

【问题讨论】:

    标签: javascript c# ajax webforms webmethod


    【解决方案1】:

    您的代码中有几个微妙的问题。

    var strTest = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note>";
    
    $.ajax({
        type: "POST",
        url: "page-methods2.aspx/GetDocument",            // no slash at start
        data: JSON.stringify({ test: strTest }),          // need to stringify
        contentType: "application/json; charset=utf-8",   // json
        dataType: "json",
        success: function(msg) {
            //debugger;
            alert(msg.d);                                 // get 'd'
        },
        failure: function(msg) {
            // failure code here
            //debugger;
            alert(msg);
        }
    });
    
    [WebMethod]
    // returning a string, so don't need this.
        //[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]    
    public static string GetDocument(string test)
    {
        string testing = test;
    
        return testing; // "This string is from Code behind";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2016-04-19
      • 2016-04-18
      • 2017-06-22
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多