简体   繁体   中英

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 have 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM