1

I am trying to call asp.net webmethod from xmlhttp object.

My webmethod is like this

[WebMethod]
public string getCities(string province)

And my javascript is like following

 xmlhttp=new XMLHttpRequest({mozSystem: true});
 xmlhttp.open("POST","http://www.rental-1.com/lp.aspx/getCities/Ontario",true);
  xmlhttp.send();
 xmlhttp.onreadystatechange=function(){
alert(xmlhttp.readyState+", "+xmlhttp.status);
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var v1=xmlhttp.responseText;
alert(v1);
}}

In response I am getting the whole html of the page. But above I am calling just webmethod and I expecting string value in return.

Any help will be appreciated. Why I am not getting just string value instead of html of the page

here is my server side code

[WebMethod]
public static List<string> getCities(string province)
{

    List<string> strp = new List<string>();



            strp=getCitiesFromDB(province);

    return strp;
}
9
  • @Quentin actually if u just read the question u will find out it not the same. Commented Mar 22, 2015 at 16:57
  • @Quentin I changed the title Commented Mar 22, 2015 at 16:58
  • Your change to the question doesn't make any sense. You make a request to the URL. You get the body of the response in a string. Since that URL gives you an HTML document you get a complete HTML document in the string. Commented Mar 22, 2015 at 16:59
  • @Quentin Yes that is the question. How can I call webmethod from xmlhttp object Commented Mar 22, 2015 at 17:02
  • You appear to be doing that already. The web method just isn't doing what you want it to do. That isn't a problem with the JavaScript. Commented Mar 22, 2015 at 17:03

1 Answer 1

1

ASP.NET will only invoke your web method if you use a post request (which you are) and send any parameters to the server as JSON (which you aren't). When those requirements aren't met, ASP.NET will just treat your request as a normal one to the underlying ASPX page. That's why you're seeing the full page's HTML returned.

If you JSON.stringify({ province: 'Ontario' }), set a content type header of application/json, and send that as your POST data, you should get a JSON array back from ASP.NET instead of the full page's HTML.

See the last section of this post for the specifics: http://encosia.com/asmx-and-json-common-mistakes-and-misconceptions/

Sign up to request clarification or add additional context in comments.

6 Comments

I am calling a method with parameter "province" which is string. My question is what will my url be. should it be rental-1.com/lp.aspx?op=getCities?v. v is json object. But it is not working
@user4545762: A URL like lp.aspx/getCities is okay, but the JSON object needs to be sent as the request's POST data, not on the querystring.
can you show me how to do it in request's POST data?
@user4545762: I don't have a raw XMLHttpRequest sample handy, but it would be $.ajax('lp.aspx/getCities', { type: 'post', contentType: 'application/json', data: JSON.stringify({ province: 'Ontario' }), success: function(response) { console.log(response); } }); using jQuery's $.ajax.
I am looking for javascript solution and not jquery. can you please let me know the pure javascript solution. Just tell me how can I embed parameter info in post data
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.