javascript 进行 Ajax 调用的替代方法有哪些?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31481965/
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-10-28 13:52:38  来源:igfitidea点击:

What are the alternative ways of making an Ajax call?

javascriptjqueryajaxjsonrest

提问by cyber8200

I'm wondering what is the best way to make an AJAX call.

我想知道进行 AJAX 调用的最佳方法是什么。

This is what I have right now, and it works just fine.

这就是我现在所拥有的,并且效果很好。

$.ajax({

    url: "/rest/computer",
    type: "GET",
    dataType: "json",

    data: {
        assessmentId: "123",
        classroomId:  "234"
    },

    success: function(objects) {


    // .... code ....


    }
});


I'm currently seeking another ways of making an Ajax call. If there is, should I use my approach ?

我目前正在寻找另一种进行 Ajax 调用的方法。如果有,我应该使用我的方法吗?

Should I move an Ajax call into it own function and call it back ?

我应该将 Ajax 调用移动到它自己的函数中并回调它吗?

Any suggestions on this will be much appreciated.

对此的任何建议将不胜感激。

回答by Bhavin Solanki

Yes there are some other ways to call ajax

是的,还有其他一些方法可以调用 ajax

jQuery

jQuery

var get_data = function(){
    var result = false;
    $.get('/rest/computer').done(function(awesome_data){
        result = awesome_data;
    });

    return result;
}

$.getJSON

$.getJSON

$.getJSON( '/rest/computer', { assessmentId:"123", classroomId:"234"})
  .done( function(resp){
    // handle response here
}).fail(function(){
   alert('Oooops');
});

If you're not using jQuery in your code, this answer is for you

如果您没有在代码中使用 jQuery,则此答案适合您

Your code should be something along the lines of this:

你的代码应该是这样的:

function foo() {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', "/rest/computer");
    httpRequest.send();
    return httpRequest.responseText;
}

var result = foo(); // always ends up being 'undefined'

回答by Umesh Markande

Following have also one of the type ajax call using post method.

以下也是使用 post 方法的 ajax 调用类型之一。

var assessmentId = "123"
var formURL = '/rest/computer';
var postData = { "action":"add","data":assessmentId};
$.post(formURL, postData, function( response ) {
    // handle response here 
});

回答by Nasser Abbassi

there is also async api that all of the modern browsers support it

还有所有现代浏览器都支持的异步 API

fetch('./api/projects',
  {
    method: 'post',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      title: 'Beispielprojekt',
      url: 'http://www.example.com',
    })
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.error(error);
  });

回答by charlietfl

Not really clear what the underlying context of your question is but for example shown you can use :

不太清楚您问题的潜在背景是什么,但例如显示您可以使用:

$.getJSON( url, { assessmentId:"123", classroomId:"234"})
  .done( function(resp){
    // handle response here
}).fail(function(){
   alert('Oooops');
});

$.getJSON is a wrapper for $.ajaxthat simplifies needing to add a number of options that are already preset

$.getJSON 是一个包装器,$.ajax它简化了添加许多已经预设的选项的需要