JSONP (JSONwithPadding) is an unofficial protocol. its implementation method is: let the client decide the Javascript function name to be called back, in the third-party server, JSON data is assembled into the callback function name, and the returned result is the jsonp output in php, the function call script with the JSON data is generally in the following format:
The code is as follows:
Callbackname ('json string ');
If the json string in the middle contains single quotation marks, this output is problematic and the caller cannot handle it. Therefore, we need to escape single quotation marks.
If json_encode is used to generate an object, you can use the following method to escape the object:
The code is as follows:
$ Ret = json_encode ($ result, JSON_HEX_APOS );
Header ('content-Type: text/javascript; charset = utf-8 ');
Echo $ callback. '(\ ''. $ ret .'\');';
Here, JSON_HEX_APOS is provided by php to replace the single quotation mark with \ u0027.
For string concatenation, you can use the following method:
The code is as follows:
$ JsonData = preg_replace ('/\'/',' \ u0027 ', $ jsonData );
And then output.