jquery ajax get example

回眸只為那壹抹淺笑 提交于 2019-11-27 17:45:40

问题


At the moment I'm using the post method like this

$.ajax({
    type: "POST",
    url: "Servicename.asmx/DoSomeCalculation", 
  data: "{param1ID:"+ param1Val+"}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        UseReturnedData(msg.d);
    },
    error: function(err) {
        alert(err.toString());
        if (err.status == 200) {
            ParseResult(err);
        }
        else { alert('Error:' + err.responseText + '  Status: ' + err.status); }
    }
}); 

Am I correct in believing that if I use a GET request instead of POST the behavior will change to being a synchronous request i.e. the execution will wait until the response has been received from the server??

Can somebody show me a jquery GET example calling a webmethod of a web service directly?

UPDATE: Using the async flag as suggested below is really all i needed to do so this works for me. I'm still curious as to what work needs to be done to the code above to make it a GET request. Changing type: "GET" doesn't have the desired effect!


回答1:


You can decide if you want the ajax call to be async or not using this:

$.ajax({
  async: false/true,
  //more options
});



回答2:


To answer your first point, no: GET and POST are independent of synchronous / asynchronous.

You can use the boolean async method to control this.




回答3:


There is a "async" flag for making the ajax call synchronous or asynchronous. You can define it as:

$.ajax({ async: false/true, //rest of code });




回答4:


look this sample maybe help you

 xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
  }
  xmlhttp.open("GET","ajax_info.txt",true);
  xmlhttp.send();


来源:https://stackoverflow.com/questions/1459211/jquery-ajax-get-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!