需要返回 jQuery.ajax 的responseText,而不是使用“成功”;功能
我想要一个函数返回 jQuery.ajax() 调用的repsonseText。我见过的所有示例都说使用“成功”函数来处理返回的数据。但是,对于我的实现,我需要如下所示的内容:
function getRemoteValue(id) {
var request = jQuery.ajax({
url:'somefile.php',
dataType:'text'
});
return request.responseText;
}
当我调用此函数时,Firebug 将请求显示为正在处理,并返回正确的响应。但是,当我尝试以下操作时,我只得到一个空字符串:
var some_value = getRemoteValue(1); // The problem is here. some_value is empty.
jQuery('.someclass').html(some_value);
// Other processing using some_value;
同样,对于我的实现,我不能执行 jQuery('.someclass').html(some_value);在 ajax() 调用中。如何获取返回的responseText?谢谢你!
I would like to have a function that returns the repsonseText of a jQuery.ajax() call. All the examples I've seen say to use a 'success' function to handle the returned data. However, for my implementation I need something like following:
function getRemoteValue(id) {
var request = jQuery.ajax({
url:'somefile.php',
dataType:'text'
});
return request.responseText;
}
When I make a call to this function, Firebug shows the request as going through with the correct Response being returned. However when I try the following, I only get an empty string:
var some_value = getRemoteValue(1); // The problem is here. some_value is empty.
jQuery('.someclass').html(some_value);
// Other processing using some_value;
Again, for my implementation I can't be doing the jQuery('.someclass').html(some_value); within the ajax() call. How can I get the responseText returned? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rober,
以下代码有效,但返回 null
ajax 调用是异步调用,它只是实例化进程继续进行,因此您将始终遇到此问题
您需要将代码移至 ajax 的成功处理程序以执行任何后期操作。
Rober ,
The following code is valid but returns null
The ajax call is asyncronous call , it just instantiates the process goes on , so you will always have this problem
You need to move your code to your success handler of ajax to do any post operations.