如何从ajax/jquery获取响应文本? [英] how do I get the reponse text from ajax / jquery?

查看:174
本文介绍了如何从ajax/jquery获取响应文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象我运行这个:

     $.ajax({
        type: 'POST',
        url: '/ajax/watch.php',
        data: {'watch':'aukcia', 'id':aukciaID},
        complete: function(responseText){
           alert(responseText);
        }
     });

在/ajax/watch.php里面,假设我有这个:

Inside /ajax/watch.php, let's say I have this:

echo 'this is what I want';

警报(responseText)返回:

And the alert(responseText) returns:

[object Object]

代替我需要的文本字符串. 有什么帮助吗?

Instead of my text string that I need. Any help, please?

推荐答案

就像您的jQuery以某种方式返回XMLHttpRequest对象而不是您的响应一样.

Looks like somehow your jQuery is returning the XMLHttpRequest object, instead of your response.

如果是这种情况,则应请求其responseText属性,如下所示:

If that is the case, you should ask for its responseText property, like this:

 $.ajax({
    type: 'POST',
    url: '/ajax/watch.php',
    data: {'watch':'aukcia', 'id':aukciaID},
    complete: function(r){
       alert(r.responseText);
    }
 });

但是,如果这不起作用,则可能实际上是在接收JSON响应,而您看到的[object Object]可能是浏览器对JSON响应的表示.

However, if that does not work, you might be actually receiving a JSON response, and the [object Object] you are seeing might be your browser's representation of your JSON response.

通过浏览对象属性,您应该能够检查其内容.但是,如果您愿意,还可以通过在呼叫中包含dataType: 'text'来告诉jQuery不要解析您的JSON响应:

You should be able to inspect its contents by navigating around the object properties. However, if you want, you can also tell jQuery not to parse your JSON response, by including dataType: 'text' on your call:

 $.ajax({
    type: 'POST',
    url: '/ajax/watch.php',
    data: {'watch':'aukcia', 'id':aukciaID},
    dataType: 'text',
    complete: function(data){
       alert(data);
    }
 });

有关更多信息,请参见: http://api.jquery.com/jQuery.ajax/

For more information, see: http://api.jquery.com/jQuery.ajax/

这篇关于如何从ajax/jquery获取响应文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆