简体   繁体   English

关于jquery.ajax()调用返回的数据的快速问题(已编辑)

[英]A quick question on data returned by jquery.ajax() call (EDITED)

EDIT: The original problem was due a stupid syntax mistake somewhere else, whicj I fixed. 编辑:最初的问题是由于其他地方的愚蠢语法错误,我已修复。 I have a new problem though, as described below 我有一个新问题,如下所述

I have the following jquery.ajax call: 我有以下jquery.ajax调用:

$.ajax({
    type: 'GET',
    url: servicesUrl + "/" + ID + "/tasks",
    dataType: "xml",
    success : createTaskListTable
});

The createTaskListTable function is defined as createTaskListTable函数定义为

function createTaskListTable(taskListXml) {
    $(taskListXml).find("Task").each(function(){  
        alert("Found task")     
    }); // each task
}

Problem is: this doesn't work, I get an error saying taskListXml is not defined. 问题是:这不起作用,我收到一条错误消息,说未定义taskListXml JQuery documentation states that the success functions gets passed three arguments, the first of which is the data. JQuery文档指出,成功函数将传递三个参数,第一个是数据。

How can I pass the data returned by .ajax() to my function with a variable name of my own choosing. 我该如何使用自己选择的变量名将.ajax()返回的数据传递给函数。

My problem now is that I'm getting the XML from a previous ajax call! 现在的问题是,我从先前的ajax调用中获取了XML! How is this even possible? 这怎么可能? That previous function is defined as function convertServiceXmlDataToTable(xml) , so they don't use the same variable name. 先前的函数定义为function convertServiceXmlDataToTable(xml) ,因此它们不使用相同的变量名。

Utterly confused. 完全困惑。 Is this some caching issue? 这是一些缓存问题吗? If so, how can I clear the browser cache to get rid of the earlier XML? 如果是这样,如何清除浏览器缓存以摆脱早期的XML?

Thanks! 谢谢!

See my comment. 看到我的评论。 If you're using IE, GET AJAX requests are cached. 如果您使用的是IE,则会缓存GET AJAX请求。 jQuery can solve this for you by adding a random querystring variable to the request. jQuery可以通过向请求中添加一个随机的querystring变量为您解决这个问题。 Simply change your AJAX call to this: 只需将您的AJAX调用更改为此:

$.ajax({
    type: 'GET',
    url: servicesUrl + "/" + ID + "/tasks",
    cache: false,
    dataType: "xml",
    success : createTaskListTable
});

That will add the random querystring automatically, thus preventing the browser from caching the request. 这将自动添加随机查询字符串,从而防止浏览器缓存请求。

Try defining your callback inline like this: 尝试像这样定义回调内联:

success: function createTaskListTable(data, textStatus, xhr) {
  console.dir(data, textStatus, xhr);
}

If data is indeed being returned as null, you might get some insight from the other fields, especially xhr. 如果确实将数据返回为null,那么您可能会从其他字段中获得一些见识,尤其是xhr。

Note that error callbacks get called with (xhr, textStatus, errorThrown). 请注意,错误回调使用(xhr,textStatus,errorThrown)进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM