【发布时间】: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