getJSON does not work

2019-07-18 21:59发布

My code in main.php is like this:

     $(document).ready(function(){
        $.getJSON('abc.php?valueOne=value1&valueTwo=value2', function(data){
            alert(data);
        }
      });

in abc.php there are text values and numbers as result which I want to display in main.php

The problem is if it is a number then it is shown in the alert alert(data) else if there are text nothing is working.

Am totally confused about this. Any solutions?

2条回答
神经病院院长
2楼-- · 2019-07-18 22:52

Replace

$.getJSON('abc.php?valueOne=value1&valueTwo=value2', function(data){
        alert(data);
    }

With

$.getJSON('abc.php?valueOne=value1&valueTwo=value2', function(data){
        alert(data);
    });

there was missing );

查看更多
聊天终结者
3楼-- · 2019-07-18 22:57

$.getJSON expects a JSON response, so you should have this kind of PHP code:

header('Content-Type: application/json');
echo json_encode(array(
    'one' => "1234",
    'two' => "Abcd",
));

Then, inside JavaScript:

function(data) {
    alert(data.one);
    alert(data.two);
}
查看更多
登录 后发表回答