gpt4 book ai didi

javascript - 进行 Ajax 调用的其他方法有哪些?

转载 作者:行者123 更新时间:2023-12-01 06:22:04 24 4
gpt4 key购买 nike

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

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

$.ajax({

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

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

success: function(objects) {


// .... code ....


}
});
<小时/>

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

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

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

最佳答案

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

jQuery

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

return result;
}

$.getJSON

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

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

你的代码应该是这样的:

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

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

关于javascript - 进行 Ajax 调用的其他方法有哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31481965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com