Sunday, March 11, 2012

Pass parameters to webservice by AJAX

Well, you know that you should pass the params first, before your oncomplete, ontimeout, onerror callbacks. In terms of making the data available to your script from the codebehind, your best option (imo) is to have on the page (either in the aspx, or as a result of registring a script block) a section o fjavascript that assigns the values to js variables. In the aspx file that'd look like this:

<script type='text/javascript'> var myVariable =<%= serverSideVariableValue%></script>

The <%= is basically a response.write call.

Retrieving from the URL, I"d rely on using hte javascript 'location' object, as in this example:http://www.eggheadcafe.com/articles/20020107.asp In short, window.location.search should return the querystring of the current URL for you.


If the service is marked as a ScriptService and the method a WebMethod, you can access the method and provide it's arguments directly. Take a look at:

http://ajax.asp.net/docs/tutorials/ExposingWebServicesToAJAXTutorial.aspx


Ok let me explain the parameter passing pattern of Web Service:

Say You have WebService SearchPeople which has the following method:

[WebMethod()]
public string[] Search(string ciy, string country)
{
//returns the result.
}

Then In JavaScript you will call the web service like the following:

PeopleService.Search('Houston', 'USA', OnComplete, OnError);

function OnComplete(result)
{
//Process your result
}

function OnError(result)
{
//Shows the error.
}

The Rule of calling a web service is first you have to pass the same number of parameters that the web service method accepts then additional OnSuccess and OnError Callback.

You can learn more on Web Service calling from this link:
http://ajax.asp.net/docs/tutorials/ASPNETAJAXWebServicesTutorials.aspx
http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

BTW the OnTimeout is no longer supported in latest version of ajax.


Ok. I followed what you said and I gave it a shot and it worked just fine. Appreciate the info. This is a [WebMethod] and there wasn't much info in the link that was provided. At least the examples were limited in info.

Since this now works I am now wondering if there is really a better way to accomplish this than the way I am. Anyone have thoughts on this.

thanks ^_^


I am not sure what you mean by limited info. But this is the way to to call a Web Service.


Actually I am having a trouble with parameters but I am not sure If I made something wrong my webmethod declared on my aspx page has mora than three parameters when I run the application its shows me an error "the parameter string can not convert to function" something like that if I remove th extra parameteres and keep only three it works fine. I would like to ask if the webmethods have a parameters restriction like number of parameters to be sent.

Thanks in advance


KaziManzurRashid:

Then In JavaScript you will call the web service like the following:

PeopleService.Search('Houston', 'USA', OnComplete, OnError);

function OnComplete(result)
{
//Process your result
}

function OnError(result)
{
//Shows the error.
}

Let's say that I need to pass parameter to OnComplete:

function OnCompleteOne(result,TagetID) {
document.getElementById(TagetID).innerHTML = result;
}

(to use the same function to populate more then one DIV)

What's the right syntex to OnCompleteOne, and PeopleService.Search?

Thanks!

Z

No comments:

Post a Comment