Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

jquery - [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

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;
        }

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?

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);
        }

    });

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!

So my question is, is [ScriptMethod(ResponseFormat = ResponseFormat.Json)] necessary on the web service definition side?

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.

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.

http://www.codeproject.com/KB/webservices/JsonWebServiceJQuery.aspx

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ResponseFormat attribute is not necessary. Including both client and server settings, only four things are required to do that:

  • Add the ScriptHandlerFactory HttpHandler in your web.config.
  • Decorate your web service(s) with the [ScriptService] attribute.
  • Request the service's methods with the POST verb.
  • Request the service's methods with a content-type of "application/json".

When you do those four things, the service methods' responses will automatically be serialized as JSON.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...