Home  >  Article  >  Backend Development  >  Problem with jquery getting json data from the server

Problem with jquery getting json data from the server

WBOY
WBOYOriginal
2016-09-08 08:44:041046browse

The same ajax method requests the json data returned by different php. It is strange that one can have a value and the other has no value. The following is the processing of my ajax method receiving return data

<code>function getUpList(_url, _u, _p){
    $.ajax({
        type:"GET",
        data:$.param({uid:_u, page:_p}),
        url:_url,
        dataType:'json',
        success:function(obj){
            alert(obj.status);
            if(obj.status == 1){
                $('div[name="data-list-p"]').append(obj.html);
            }
        }
    });
}</code>

I can be very sure that the data obtained by obj is indeed in json format. But the alert is always undefined, which means it is very confusing!

Reply content:

The same ajax method requests the json data returned by different php. It is strange that one can have a value and the other has no value. The following is the processing of my ajax method receiving return data

<code>function getUpList(_url, _u, _p){
    $.ajax({
        type:"GET",
        data:$.param({uid:_u, page:_p}),
        url:_url,
        dataType:'json',
        success:function(obj){
            alert(obj.status);
            if(obj.status == 1){
                $('div[name="data-list-p"]').append(obj.html);
            }
        }
    });
}</code>

I can be very sure that the data obtained by obj is indeed in json format. But the alert is always undefined, which means it is very confusing!

It’s so amazing, the questioner, are you sure that the two requested are standard jsondata! If they are all standard, there should be no undefined. Try adding eval!

<code>success:function(obj){
    obj = eval("("+obj+")");
    alert(obj.status);
    if(obj.status == 1){
        $('div[name="data-list-p"]').append(obj.html);
    }
}</code>

Have you got the data? You can check it in the chrome console

Is the data format returned by the two URLs wrong? Check if json is not standard

What should be returned is not the JSON format, but the JSON string, just convert it

Both chrome and firefox can see the returned parameters. Very good confirmation! ~
Don’t rush to output .status. It’s better to output the entire obj first and then see the result~!

Change the code for php to return json to the same and take a look

Hmm. I am also sure that what you got is jsondata, but does this obj really have the status attribute?


console.log(obj); It will be more convenient to view it on the console in this way.

<code class="javascript">var obj_json =  JSON.parse(obj);</code>

String becomes json

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn