ASP.NET的WebService披着我的XML标记JSON响应 [英] ASP.NET WebService is Wrapping my JSON response with XML tags
问题描述
我不知道我要去哪里错了我失去了我的。
我建立一个ASP.NET 2.0(在.NET 3.5框架)的Web应用程序,我包括web服务。注意,这是的不的一个MVC项目。我想揭露它会返回一个JSON字符串的方法;格式化喂jqGrid的jQuery插件。
这是preliminary测试方法我在我的服务中实现:由于(的菲尔哈克指南MVC )
[的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共字符串的getData()
{
SER的JavaScriptSerializer =新的JavaScriptSerializer(); VAR jsonData =新
{
总= 1,//我们将在后面实现
页面= 1,
记录= 3,//后实施
行=新的[] {
新{ID = 1,细胞=新[] {1,-7,这是一个很好的问题?,耶}},
新{ID = 2,电池=新[] {2,15,这是明目张胆的骗人货?,耶}},
新{ID = 3,电池=新[] {3,23,为什么天空是蓝色的?,耶}}
}
}; 返回ser.Serialize(jsonData); //products.ToString();
}
在调用此函数返回(格式化为清晰起见):
<?XML版本=1.0编码=UTF-8&GT?;
<字符串mlns =http://tempuri.org/>
{
总:1,
页:1,
纪录:3,
行:
[
{ID:1,细胞:1, - 7,这是一个很好的问题?,耶]},
{ID:2,细胞:2,15,这是明目张胆的骗人货?,耶]},
{ID:3,细胞:3,23,为什么天空是蓝色的?,耶]}
]
}
< /串>
我将如何实现上述反应的没有 XML的包装材料?
三样东西,你可能不这样做的:
- 标记方法静态
- 执行POST
- 手空{}在jQuery的数据。
有可能是一个方法来调用该方法用GET,我只用过POST。我能得到你的例子有这方面的工作:
<脚本SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js>< / SCRIPT&GT ;
<脚本>
//在JavaScript块
$(文件)。就绪(函数()
{
$阿贾克斯({
网址:/Default.aspx/Tester
键入:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
数据:{},
成功:完成
});
}); 功能完成(数据)
{
//包含http://www.json.org/json2.js如果你的浏览器不支持原生JSON
VAR数据= JSON.parse(data.d);
警报(data.total);
}
< / SCRIPT>
背后的code(你并不需要创建一个web服务,你可以把这个在你的Default.aspx):
[的WebMethod]
公共静态字符串测试仪()
{
SER的JavaScriptSerializer =新的JavaScriptSerializer(); VAR jsonData =新
{
总= 1,//我们将在后面实现
页面= 1,
记录= 3,//后实施
行=新的[] {
新{ID = 1,细胞=新[] {1,-7,这是一个很好的问题?,耶}},
新{ID = 2,电池=新[] {2,15,这是明目张胆的骗人货?,耶}},
新{ID = 3,电池=新[] {3,23,为什么天空是蓝色的?,耶}}
}
}; 返回ser.Serialize(jsonData); //products.ToString();
}
结果:
<$p$p><$c$c>{\"d\":\"{\\\"total\\\":1,\\\"page\\\":1,\\\"records\\\":3,\\\"rows\\\":[{\\\"id\\\":1,\\\"cell\\\":[\\\"1\\\",\\\"-7\\\",\\\"Is这是一个很好的问题\\,\\耶\\]},{\\ID \\?:2,\\单元格\\:[\\2 \\,\\十五\\,\\这是明目张胆的?骗人货\\,\\耶\\]},{\\ID \\:3,\\单元格\\:[\\3 \\,\\23 \\,\\天空为什么是蓝色的\\? ,\\好极了\\]}]}}一个更详细的解释是<一个href=\"http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/\">here
I'm not sure where I'm going wrong of what I'm missing.
I'm building an ASP.NET 2.0 (on the .Net 3.5 framework) Web application and I am including a webservice. Note that this is not an MVC project. I wish to expose a method which will return a JSON string; formatted to feed the jqGrid jQuery plugin.
This is the preliminary test method I've implemented in my service: thanks to (Phil Haack's Guide for MVC)
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getData()
{
JavaScriptSerializer ser = new JavaScriptSerializer();
var jsonData = new
{
total = 1, // we'll implement later
page = 1,
records = 3, // implement later
rows = new[]{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?", "yay"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?", "yay"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?", "yay"}}
}
};
return ser.Serialize(jsonData); //products.ToString();
}
When invoked this is returning (formatted for clarity):
<?xml version="1.0" encoding="utf-8" ?>
<string mlns="http://tempuri.org/">
{
"total":1,
"page":1,
"records":3,
"rows":
[
{"id":1,"cell":["1","-7","Is this a good question?","yay"]},
{"id":2,"cell":["2","15","Is this a blatant ripoff?","yay"]},
{"id":3,"cell":["3","23","Why is the sky blue?","yay"]}
]
}
</string>
How would I achieve the above response without the xml wrappings?
Three things you may not be doing:
- Marking the method static
- Performing a POST
- Hand an empty "{ }" for the data in jQuery.
There may be a way to call the method with a GET, I've only ever used POST. I was able to get your example working with this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
// In your javascript block
$(document).ready(function()
{
$.ajax({
url: "/Default.aspx/Tester",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{}",
success: done
});
});
function done(data)
{
// Include http://www.json.org/json2.js if your browser doesn't support JSON natively
var data = JSON.parse(data.d);
alert(data.total);
}
</script>
The code behind (you don't need to create a webservice, you can put this in your default.aspx):
[WebMethod]
public static string Tester()
{
JavaScriptSerializer ser = new JavaScriptSerializer();
var jsonData = new
{
total = 1, // we'll implement later
page = 1,
records = 3, // implement later
rows = new[]{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?", "yay"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?", "yay"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?", "yay"}}
}
};
return ser.Serialize(jsonData); //products.ToString();
}
The result:
{"d":"{\"total\":1,\"page\":1,\"records\":3,\"rows\":[{\"id\":1,\"cell\":[\"1\",\"-7\",\"Is this a good question?\",\"yay\"]},{\"id\":2,\"cell\":[\"2\",\"15\",\"Is this a blatant ripoff?\",\"yay\"]},{\"id\":3,\"cell\":[\"3\",\"23\",\"Why is the sky blue?\",\"yay\"]}]}"}
A more detailed explanation is here
这篇关于ASP.NET的WebService披着我的XML标记JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!