开发者

ASP.net Script Service consumption in javascript

I have a script service in asp.net and I need to consume this WS from javascript using JSONP (Script tag injection; since it is cross domain, no $.ajax() call).

In this case, where the input to the web method is a complex structure, I have to create the input structure at the client. How does matching of the client-side structure to the Server side parameter happen?

To make the question a bit more clearer for the unapt answers given :-

Assume that I ha开发者_开发问答ve somehow created the complex input and passed it to the Script Method. Now, how does the matching/vaidation of my input structure to the input parameter of the Script method happen (or in other words, what is the basis of the matching?)


You can pass complex types to a webservice without using the Microsoft.Ajax framework. I wrote a little example using JQuery.

Assume you have the following C# class used as parameter for the webmethod:

namespace JQueryWebServiceTest
{
    public class TwoStringsTogether
    {
        public string StringA;
        public string StringB;
    }
}

The webmethod has the following signature:

TwoStringsTogether TransformTwoStringsTogether(TwoStringsTogether input)

You can call the method like this:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "TestService.asmx/TransformTwoStringsTogether",
    data:"{" +
        "input: {" +
        "__type: 'JQueryWebServiceTest.TwoStringsTogether'," +
        "StringA: 'HalloA'," +
        "StringB: 'HalloB'" +
        "}" +
    "}",
    dataType: "json",
    success: SuccessCallback
});

Note the __type parameter, without that it won't work.


In this case you will sending a JSON structure based on your discovery of the parameters. If it is same as what service accepts then your call will succeed else fail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜