简体   繁体   中英

Can't get JSON from php script but can get it from file.json

This is what my testing php script is doing:

$json = array (
    "age" => 5,
    "bob" => "Lee",
);  
$json = json_encode($json);

echo $json;

It prints out the json fine as when I output its content into a file.json and read it from there, the ajax will pick it up.

Ajax part:

searchJson(): any 
{   
    let items: any = []; 
    $.getJSON('app/php/search.php', 
        function(data)
        {   
            $.each(data, function (key, val)
            {   
                console.log(key + " : " + val);
                items.push(`${key} : ${val}`);
            }); 
        }); 
    return items;
} 

Changing 'app/php/search.php' to 'file.json' will return in a non-empty return, whereas now it returns nothing.

Turns out it was related to running from Angular 2's given address of 10.xxx:port. When it's run from www.something.com it will show up correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM