jquery ajax获取示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1459211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:30:42  来源:igfitidea点击:

jquery ajax get example

jqueryajaxpostget

提问by user48408

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

目前我正在使用这样的 post 方法

$.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??

我是否认为如果我使用 GET 请求而不是 POST 行为将更改为同步请求,即执行将等到从服务器收到响应?

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

有人可以向我展示一个直接调用 Web 服务的 webmethod 的 jquery GET 示例吗?

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!

更新:使用下面建议的 async 标志真的是我需要做的一切,这对我有用。我仍然很好奇需要对上面的代码做哪些工作才能使其成为 GET 请求。更改类型:“GET”没有预期的效果!

采纳答案by peirix

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

您可以决定是否希望 ajax 调用异步或不使用:

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

回答by Greg

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

要回答您的第一点,否:GET 和 POST 独立于同步/异步。

You can use the boolean asyncmethod to control this.

您可以使用布尔async方法来控制它。

回答by Moulesh Kumar

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

有一个“异步”标志用于使 ajax 调用同步或异步。您可以将其定义为:

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

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

回答by Ashouri

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();