【问题标题】:Javascript send Json DataJavascript 发送 Json 数据
【发布时间】:2015-12-07 13:22:24
【问题描述】:

如何通过 ajax 发送 json 字符串并将 json 字符串转换为 xml?

错误:加载资源失败:服务器响应状态为 500(内部服务器错误)

$.ajax({
                async: true,
                url: "WebForm1.aspx/btnSave",
                type: "POST",
                data: "{data: '" + "{'?xml': {'@version': '1.0'},'Card': { 'Main_Client_Information': {'Surname': '','Name': '','Middle_name': '','Full_name': '','Short_name': '','RNN': '','IIN': '','Birthday': '','Doc_Type': {'@code': ''}}}}" + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert(response.d);
                },
                error: function (error) {
                    debugger;
                    alert(error);
                }
            });

如果发送 $.ajax data: '{data: "something"} - 完美运行,如何使用 "json like string" 代替 "something"

WebForm.aspx.cs

[System.Web.Services.WebMethod]
    public static string btnSave(string data)
    {

        string response = "";

        try
        {

            XmlDocument xmlDocument = (XmlDocument)JsonConvert.DeserializeXmlNode(data);

            xmlDocument.Save(Server.MapPath("output.xml"));

            response = "success";

        }
        catch (Exception ex)
        {
            response = "error" + ex.Message;
        }

        return response;

    }

我只想得到这个字符串 ---------> "{'?xml': {'@version': '1.0'},'Card': { 'Main_Client_Information': {'Surname' : '','Name': '','Middle_name': '','Full_name': '','Short_name': '','RNN': '','IIN': '','Birthday': '','Doc_Type': {'@code': ''}}}}" + "'}" ------------ in webmethod btnSave 并转换转xml格式

【问题讨论】:

  • 你调试过btnSave吗?因为那时你的异常被抛出了。
  • 数据没有发送到服务器
  • 这里的答案有帮助吗? stackoverflow.com/questions/5092352/…
  • 我不认为这是您的问题的原因,但您的 JSON 字符串缺少右括号。它应该像 {'@code': ''}}}}"; 一样结束。
  • 为什么要将数据作为字符串发送?如果您出于某种原因需要将 json 构建为字符串,您始终可以使用 JSON.parse()(大多数浏览器都支持)在客户端解析它

标签: javascript c# asp.net json


【解决方案1】:

问题在于您告诉 jQuery 要发布哪些数据的方式:

您可能会感到困惑,因为传递给$.ajax 的参数有一个名为data 的属性。 您现在正在向其中传递一个字符串,而您应该传递一个 Json 字典,其中包含您要发送的变量名称和值。

试试这个:

您的整个通话应如下所示:

(为了清楚起见,我将您尝试发送的数据保存在单独的变量中)

var stringData ="{'?xml' : '@version': '1.0'},'Card': { 'Main_Client_Information': {'Surname': '','Name': '','Middle_name': '','Full_name': '','Short_name': '','RNN': '','IIN': '','Birthday': '','Doc_Type': {'@code': ''}}}}";

$.ajax({
    async: true,
    url: "WebForm1.aspx/btnSave",
    type: "POST",
    data: {data:stringData},
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        alert(response.d);
    },
    error: function (error) {
        debugger;
        alert(error);
    }
});  

【讨论】:

    【解决方案2】:

    您不能将数据作为 json 字符串发送。你需要调用 JSON.parse (json_string_here_)。

    您也可能需要 put 而不是 post,但我不能确定,因为我不知道您是在执行插入还是更新。

    对不起,即使在那之后我不得不说,如果你能发送一个这样的 xml 文件,我会有点印象深刻。我想这不会很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-26
      • 2012-08-11
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      相关资源
      最近更新 更多