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;
}
user4545762
  • 59
  • 1
  • 8
  • @Quentin actually if u just read the question u will find out it not the same. – user4545762 Mar 22 '15 at 16:57
  • @Quentin I changed the title – user4545762 Mar 22 '15 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. – Quentin Mar 22 '15 at 16:59
  • @Quentin Yes that is the question. How can I call webmethod from xmlhttp object – user4545762 Mar 22 '15 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. – Quentin Mar 22 '15 at 17:03
  • @Quentin if you know can u please answer it how can I call webmethod and get just return value of webmethod. I am kind of confused – user4545762 Mar 22 '15 at 17:25
  • You should probably include more than 2 lines of your server side code if you want anyone to tell what is wrong with it. – Quentin Mar 22 '15 at 17:33
  • http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ might be relevent. – Quentin Mar 22 '15 at 17:36
  • @Quentin ok I updated my code. added server side getCities method. – user4545762 Mar 22 '15 at 17:37

1 Answers1

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/

Dave Ward
  • 57,126
  • 11
  • 114
  • 134
  • 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 Mar 23 '15 at 09:13
  • @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. – Dave Ward Mar 23 '15 at 13:22
  • can you show me how to do it in request's POST data? – user4545762 Mar 23 '15 at 13:30
  • @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`. – Dave Ward Mar 23 '15 at 14:10
  • 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 – user4545762 Mar 23 '15 at 15:02
  • @user4545762: It's been so long since I've used XMLHttpRequest by hand, I don't remember the exact syntax. You might want to put that in a separate question. It will work once you make your request like I've explained in the answer above. This question is probably a good start (but make sure you don't use that Content-Type): http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest – Dave Ward Mar 23 '15 at 16:56