[ScriptMethod(ResponseFormat = ResponseFormat.Json) [英] [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
问题描述
在如果没有指定以上ASP.net Web服务,什么是默认响应格式? 另外,如果我下面的Web服务:
In ASP.net web service if the above isn't specified , what is the response format by default? Also, if my web service below:
[WebMethod()]
public List<Sample> GenerateSamples(string[][] data)
{
ResultsFactory f = new ResultsFactory(data);
List<Sample> samples = f.GenerateSamples();
return samples;
}
返回对象的列表,如果我改变的响应格式为JSON,我必须要改变的返回类型为字符串,那我怎么访问我的JavaScript对象?
returns the list of objects, If I change the response format to JSON, I have to change the return type to string, then how do I access objects in my javascript?
目前我在JS调用这个Web服务,如:
Currently I call this web service in my JS such as:
$.ajax({
type: "POST",
url: "http://localhost/TemplateWebService/Service.asmx/GenerateSamples",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var samples = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
if (samples.length > 0) {
doSomethingHere(samples);
} else {
alert("No samples have been generated");
}
},
error: function(xhr, status, error) {
var msg = JSON.parse(xhr.responseText);
alert(msg.Message);
}
});
我注意到,虽然,即使一切工作完全正常,eval语句永远不会被执行,这意味着网络服务始终返回一个字符串!
What i noticed though, even though everything works perfectly fine, the eval statement never gets executed, which means that the web service always returns a string!
所以我的问题是,是[ScriptMethod(ResponseFormat = ResponseFormat.Json)需要在Web服务定义的一面?
So my question is, is [ScriptMethod(ResponseFormat = ResponseFormat.Json)] necessary on the web service definition side?
该办法的事情,现在,我可以用的样品阵列和访问每个对象及其属性,我通常会在任何OOP code,这是非常方便的,而且一切正常没有问题,但我只是想确保我不会错过我的设置任何东西。
The way things are now, I can use samples array and access each object and its properties as I normally would in any OOP code, which is very convenient, and everything works no problem, but I just wanted to make sure that I am not missing anything in my set up.
我把结合jQuery的阿贾克斯与Encosia侧asp.net的基础知识,并响应类型中没有提到有 - 我读了它在其他网站和我我不知道这是多么重要。
I took the basics of combining Jquery's ajax with asp.net from Encosia side, and the response type wasn't mentioned there - I read it on another site and am I not sure how vital it is.
<一个href="http://www.$c$cproject.com/KB/webservices/JsonWebServiceJQuery.aspx">http://www.$c$cproject.com/KB/webservices/JsonWebServiceJQuery.aspx
列出了asp.net web服务端4种不同的变化。我只有第2 - 在我的web.config。服务本身和样品类是没有任何序列化实现的,它确实有属性虽然。我猜的Web服务是JSON在默认情况下?而只要你的对象有属性,它们是序列化的默认?这是我的理解,直到我读了这篇文章。
Lists 4 different changes on the asp.net web service side. I only have the first 2 - in my web.config. The service itself and the Sample class is implemented without any serialization, it does have properties though. I guess the web service is JSON by default? And as long as your objects have properties, they are serializable by default? That was my understanding until I read this article.
推荐答案
该ResponseFormat属性是没有必要的。包括客户端和服务器的设置,只有四件事情需要做:
The ResponseFormat attribute is not necessary. Including both client and server settings, only four things are required to do that:
- 添加ScriptHandlerFactory的HttpHandler在你的web.config。
- 用[ScriptService]属性装饰你的Web服务(S)。
- 在请求服务的方法与POST动词。
- 在请求服务的方法与内容类型application / JSON的。
当你做那些四件事,服务方法的反应会自动序列化为JSON。
When you do those four things, the service methods' responses will automatically be serialized as JSON.
这篇关于[ScriptMethod(ResponseFormat = ResponseFormat.Json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!