简体   繁体   中英

JSONP not working on json converted url

The answer from this question relating to JSONP does work as of. If there is a ? at the end of a url during .getJSON request it recognizes as a JSONP.

However in my case it is not working. I have a url lets say (sanjokgrg.com/aboutme) which displays the whatever data queried from the about table, I also added an extra feature where it looks for json=true in the URL which converts the result into json this way print json_encode($data); in php. So sanjokgrg.com/aboutme displays the normal data while sanjokgrg.com/aboutme?json=true displays the json data but getJson does not recognize sanjokgrg.com/aboutme?json=true and I tried a placeholder api https://jsonplaceholder.typicode.com/comments and it works only my ?json=true not working

I finally made it work. Thanks to @Felix Kling for some insight regarding JSONP. Looks like I should have looked more into understanding the concept of JSONP. While it is true that adding a ? at the end of the .getJson request makes the request a JSONP, that does not necessarily mean the server will comply to that request. So the server needs to account for the "padding". I had php source code to my server so I was able to amend the output by doing this.

echo header('Content-Type: application/json');

echo htmlspecialchars($_REQUEST['callback'], ENT_QUOTES, 'UTF-8')."(".json_encode($data).");";

On hindsight should have been more of a php question.

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