I need to take some data on WP from OSStore

I am writing the following:

$.ajax({ type: "POST", url: "http://test.pp.ua/index.php?route=common/forBlog&callback=?", dataType: 'jsonp', data: {'key': 1}, cache: false, crossDomain: true, success: function (data){ console.log(data); } }); 

The following file is written to the file:

 header('Access-Control-Allow-Origin: *'); $data = array( 'test1' => 12, 'test2' => 'good' ); $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($data)); 

In Console I get the following: enter image description here

and the body itself with the data that the file returned enter image description here

Tell me how to solve this problem? Thank you for your time.

  • function () {console.log(data);} typo - saaaaaaaaasha
  • Nothing changed. Everything remains as before - mydls1
  • @ mydls1 you know what makes JSON different from JSONP? - Dmitriy Simushev
  • According to the structure, as far as I know, nothing. jsonp calls the callback function to exchange data between different domains. Probably those aspects that you mean - I do not know. - mydls1

1 answer 1

Some lyrics.

JSONP format assumes the presence of a wrapping function around the transmitted data. This allows you to use the JSONP response as a full-fledged JavaScript code and use it in the body of the <script> . This feature allows you to make cross-domain queries.

A typical example of a server response in JSONP is:

 callback123({"foo": "bar"}); 

Unlike JSONP, JSON format does not contain a wrapping function and, strictly speaking, is not a fully valid JavaScript code. A typical server response in JSON format is:

 {"foo": "bar"} 

If you put this answer in the <script> then the JavaScript parser should give you a syntax error ( SyntaxError ).


Now about your problem

In the client code, you require a response from the server in JSONP format, and return the usual JSON. This causes the problem (the reason is described above).

To make it work, it’s enough to give a valid JSONP:

 header('Access-Control-Allow-Origin: *'); $this->response->addHeader('Content-Type: application/javascript'); $data = array( 'test1' => 12, 'test2' => 'good' ); $this->response->setOutput(sprintf( '%s(%s);', $this->request->get['callback'], json_encode($data) )); 
  • Now I will try, and you can give a link to the resource, where does the information about jsonp come from? I am pleased to get acquainted. - mydls1
  • @ mydls1 ru.wikipedia.org/wiki/JSONP - Dmitriy Simushev
  • @ mydls1, I think that's enough for a start. In general, there is a lot of information on the JSONP network - just look for it - Dmitriy Simushev