1

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

Sanjok Gurung
  • 948
  • 4
  • 17
  • 33
  • that website doesnt exist, or is that just an example? – dangee1705 Jan 02 '18 at 11:12
  • I was only using it to explain the question. I added "lets say" in my question. – Sanjok Gurung Jan 02 '18 at 11:14
  • JSON and JSONP are not the same thing. The server has to be able to generate JSONP, not JSON. – Felix Kling Jan 02 '18 at 11:37
  • @FelixKling Please Look at the documentation http://api.jquery.com/jQuery.getJSON/ says it being a JSONP when ? is attached at the end – Sanjok Gurung Jan 02 '18 at 11:39
  • I understand. But then the server has to send JSON**P** back, *not* JSON. If the server does not support JSON**P** you cannot *request* JSON**P**. You are saying *" I also added an extra feature where it looks for json=true in the URL which converts the result into json"* to me sounds like you are not sending JSONP back. However, before you go through the trouble to add JSONP support to the server: Do you need JSONP support in the first place? – Felix Kling Jan 02 '18 at 11:40
  • @FlexKling It works when Im using https://jsonplaceholder.typicode.com/comments external JSON url. DOes that mean I have to take into account for a JSONP somehow with my PHP ?? – Sanjok Gurung Jan 02 '18 at 11:43
  • 1
    You should have a look at the actual request and response that is sent to the example server. jQuery will actually request something like https://jsonplaceholder.typicode.com/comments?callback=someCallback . As you can see the result is not JSON. Maybe have a look at https://en.wikipedia.org/wiki/JSONP to learn more about how JSONP works and whether you actually need it. *"DOes that mean I have to take into account for a JSONP somehow with my PHP"* Yes. – Felix Kling Jan 02 '18 at 11:45
  • @FelixKling Thanks. I made it work, have a look at my answer below. – Sanjok Gurung Jan 02 '18 at 13:21

1 Answers1

0

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.

Sanjok Gurung
  • 948
  • 4
  • 17
  • 33