Javascript 在AJAX中调用JSON成功
我想在我的AJAX中调用json数据。我对处理json和AJAX还是新手。有人能帮助我如何访问来自另一个URL的json数据吗?我想将Javascript 在AJAX中调用JSON成功,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我想在我的AJAX中调用json数据。我对处理json和AJAX还是新手。有人能帮助我如何访问来自另一个URL的json数据吗?我想将id与JSON数据进行比较。以下是我目前的代码: function getCard(id){ $.ajax({ type: "GET", data: "id=" + id, success: function(data){
id
与JSON数据进行比较。以下是我目前的代码:
function getCard(id){
$.ajax({
type: "GET",
data: "id=" + id,
success: function(data){
#call JSON data here from another URL
# Is it possible to call another AJAX here?
}
});
}
是的,祖玛,您可以在Ajax调用的成功函数中调用另一个Ajax。下面是一个例子:
$.ajax({
type: "post",
url: url1,
data: data1,
success: function(data){
$.ajax({
type: "post",
url: url2,
data: data2,
success: function(data){
});
});
});
功能卡(id){
此代码将适用于您
$.ajax({
url: "url",
method: "post",
data: "id=" + id,
success:function(data) {
// success goes here
$.ajax({
url: "url",
async:false,
method: "post",
data: "id=" + id,
success:function(json) {
JSON.parse(json);
// compare data and json here
},
error: function(){
// error code goes here
}
});
},
error: function(){
// error code goes here
}
});
你为什么不试试呢?我会使用回调函数来保持一切干净。这是可能的。尝试嵌套另一个
$.ajax
调用。你的url在哪里?如果服务器端逻辑正确,你的json将是传递给success函数的参数,在本例中是data
$.ajax({
url: "url",
method: "post",
data: "id=" + id,
success:function(data) {
// success goes here
$.ajax({
url: "url",
async:false,
method: "post",
data: "id=" + id,
success:function(json) {
JSON.parse(json);
// compare data and json here
},
error: function(){
// error code goes here
}
});
},
error: function(){
// error code goes here
}
});