• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

Consuming ASP.NET web service with jQuery on remote web service

I'm new to jQuery, and I need to find out how to consume a web service using jQuery from an asp.net page. I'd appreciate any guidance on how to do this, and how to deal with the Same Origin Policy (which I understand is an issue).

The web method takes two complex objects (each with multiple properties) and multiple strings as parameters and returns a list of objects with multiple properties.

Thanks in advance.
0
Ast35
Asked:
Ast35
  • 4
  • 4
1 Solution
 
TheAvengerCommented:
Have a look here: http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/.

You can also google "ASP.NET AJAX jQuery call web service", which will give you good results.
0
 
Ast35Author Commented:
OK, so what I would do in C# is to create the two proxy objects that I'm going to pass to the web method and then set their properties before dealing with the results.

            ComplexObjectTypeA complexObject1 = new ComplexObjectTypeA();
            complexObject1.value1 = "data";
            complexObject1.value2 = "data";
            complexObject1.value3 = "data";


            ComplexObjectTypeB complexObject2 = new ComplexObjectTypeB();
            complexObject2.value1 = "data";
            complexObject2.value2 = "data";
            complexObject2.value3 = "data";


ResultList testResults = new ResultList();
           testResults = ws.getResults(stringA, stringB, ComplexObject1, ComplexObject2, StringC, StringD, StringE, StringF);

            foreach (Result est in testResults.lst)
            {
                LabelTestResults.Text +=  est.lsdResult + " " + est.lsdFirstName + " " + est.lsdLastName + "<br>";
            }



From that article and others I have read, I think I understand how to pass string values to the web method (kind of like below?), but not really how to deal with the creation of the proxy complex objects like (complexObject1 and complexObject2 above) and passing them in that way in jQuery.

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "WebService.asmx/GetResults",
  data: "{'stringA':'data', 'stringB':'data'}",
  dataType: "json"
});

0
 
TheAvengerCommented:
Give it a try with only one and more simple object first. You should change this part: data: "{'stringA':'data', 'stringB':'data'}", with something like:

data: "{'value1':'data', 'value2':'data'...},{...}"

The {..} defines an object, the 'name':'value' defines a public property and its value.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ast35Author Commented:
Thanks!

So names of the types of complex object that I'm passing don't need to be given, all I need to do is pass the properties within the curly brackets in the order that they appear in the method description?

Does a single string value get its own set of curly brackets separated from the previous object?
0
 
Ast35Author Commented:
There is a real web service I am trying to consume by the way. I was using those names for illustrative purposes. :)
0
 
TheAvengerCommented:
The order of the properties is not important, you can give them in any order you want.
The curly brackets define an object. If you have just a string, you don't need to make it an object, just provide it as a string in quotation marks
0
 
Ast35Author Commented:
I think I see, thanks.

Just to make sure I understand correctly, the ScriptManager control gets around the Same Origin Policy by making sure the request is an HTTP POST request and that the content type is JSON, right?
0
 
TheAvengerCommented:
I am not sure about that, you should try it and see if some errors appear and then google them.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now