开发者

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

开发者 https://www.devze.com 2023-02-17 18:23 出处:网络
Imagine I run this: $.ajax({ type: \'POST\', url: \'/ajax/watch.php\', data: {\'watch\':\'aukcia\', \'id\':aukciaID},

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:

echo 'this is what I want';

And the alert(responseText) returns:

[object Object]

Instead of my text string that I need.开发者_StackOverflow中文版 Any help, please?


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

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);
    }
 });

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.

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);
    }
 });

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


Use on your client side ajax like this

 $.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);
  }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消