最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to alert ajax response - Stack Overflow

matteradmin1PV0评论

I've read dozens of related posts, but I still can't get this to work.

I want to alert the response in jquery I get from PHP.

PHP:

$msg=array();

    if(empty($whatever)){
        $msg['cenas']="Não há contas";
    }else{
        $msg['cenas']="Há contas";
    };

    echo json_encode($msg);

JS:

$.ajax({
    url: 'myscript.php',
    dataType: 'json',
    success: function(response){
       alert(response.cenas);
    }
});

PHP is echoing

{cenas: "Há contas}" But I can't get it to alert in JS.

I've read dozens of related posts, but I still can't get this to work.

I want to alert the response in jquery I get from PHP.

PHP:

$msg=array();

    if(empty($whatever)){
        $msg['cenas']="Não há contas";
    }else{
        $msg['cenas']="Há contas";
    };

    echo json_encode($msg);

JS:

$.ajax({
    url: 'myscript.php',
    dataType: 'json',
    success: function(response){
       alert(response.cenas);
    }
});

PHP is echoing

{cenas: "Há contas}" But I can't get it to alert in JS.

Share Improve this question edited Feb 7, 2014 at 22:39 asked Feb 7, 2014 at 22:22 user882670user882670 3
  • 3 using firebug or chrome developer tools log the alert instead as console.log(response). That will reveal your entire object response and should let you see what's wrong. – Harvey A. Ramer Commented Feb 7, 2014 at 22:25
  • @HarveyA.Ramer console.log(response) returns "success" – user882670 Commented Feb 7, 2014 at 22:44
  • 1 That means, I believe, that you are just returning a string from your myscript.php file on success. In that case, you'll need to modify it to return an object to do what you're trying to do. This might be instructive: stackoverflow./questions/8649621/… – Harvey A. Ramer Commented Feb 8, 2014 at 3:08
Add a ment  | 

3 Answers 3

Reset to default 4

The php should echo back {"cenas": "Há contas"}, but what did you get in the alert? Did you get an undefined? If so, try to use jQuery.parseJSON before alert. e.g:

$.ajax({
    url:"myscript.php",
    dataType: "json",
    success:function(data){
       var obj = jQuery.parseJSON(data);
       alert(obj.cenas);
    }
});

You should tell jQuery to expect (and parse) JSON in the response (although jQuery could guess this correctly...) and you should write your javascript correctly:

$.ajax({
    url: 'myscript.php',
    dataType: 'json',
    success: function(response){
       alert(response.cenas);
    }
});

Try

$.ajax({
    url:"myscript.php",
    dataType: "json",
    success:function(data){
       alert(data.cenas);
    }
});

You have a syntax error.

Check out the Docs for $.ajax

Post a comment

comment list (0)

  1. No comments so far