如何从 ajax/jquery 获取响应文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5366900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:02:09  来源:igfitidea点击:

how do I get the reponse text from ajax / jquery?

ajaxjquery

提问by Richard Rodriguez

Imagine I run this:

想象一下我运行这个:

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

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

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

echo 'this is what I want';

And the alert(responseText) returns:

警报(响应文本)返回:

[object Object]

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

而不是我需要的文本字符串。请问有什么帮助吗?

回答by Fábio Batista

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

看起来您的 jQuery 以某种方式返回 XMLHttpRequest 对象,而不是您的响应。

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

如果是这种情况,您应该询问它的responseText属性,如下所示:

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

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.

但是,如果这不起作用,您实际上可能会收到 JSON 响应,而[object Object]您看到的可能是浏览器对 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:

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

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

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

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

回答by Ram

Use on your client side ajax like this

像这样在您的客户端 ajax 上使用

 $.ajax({
        type: "POST",
        url: "insert-data.php",
        data: 
    {student_name:student_name,student_roll_no:student_roll_no
     ,student_class:student_class},
        dataType: "JSON",
        success: function(data) {
         $("#message").html(data);
        $("p").addClass("alert alert-success");
        },
        error: function(err) {
        alert(err);
        }
    });

in server side after query excecute you may use it give success when you query success false when your query has fault

在查询执行后的服务器端,当您查询有错误时,您可以使用它获得成功

if($stmt->execute())
 {
$res="Data Inserted Successfully:";
 echo json_encode($res);
}
 else {
 $error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
  }

回答by Bruce Tong

I think you are receiving this in your server respones

我认为您在服务器响应中收到了这个

{message:'hello world'}

if thats the case then use

如果是这样,那么使用

JSON.parse(data.responseText).message

to convert the json string into javascript object and access your message property.

将 json 字符串转换为 javascript 对象并访问您的消息属性。