开发者

Single quotes in JSON from PHP causing problems

NOTE: this was a completely different question until I realized where the problem really was.

My current issue is that I am trying to output some JSON from PHP for use by jQuery. I am doing this cross-domain so I am using "JSONP"开发者_开发百科. I have narrowed the problem down to the fact that there are single quotes in my JSON so when I output with the callback function I end up getting too many single quotes.

I have tried calling str_replace("'","\'",$value) in PHP and it seems to output as \\' in my JSON rather than \' which is apparently not readable by jQuery (though online JSON validators say the JSON is valid.

So what I need to know is how to get only a single slash in my string inside of PHP rather than 2 slashes.


What I'm gathering is this:

jQuery uses _ as a callback parameter to JSONP requests. It will automatically append ?_=jQuery<random_numbers> basically telling the server to call this unique function name when it returns. As such, as of recently (within the last year I want to say) .ajax takes care of making a callback function for you, then kind of "re-routes" it to a function you specify within the success property.

Also, your PHP code is using $_GET['callback'] when it now should be using $_GET['_'] (to stay more in-line with jQuery and what it's sending).

The "jQuery was not called" is just jQuery notifying you that anticipated callback wasn't included in the response, and it was looking to make sure it was getting called.

Short answer (as I see it) reference $_GET['_'] instead of $_GET['callback'] to satisfy jQuery.


try to use this in your PHP:

echo $_GET['callback'] . '(' . json_encode($your_array_data) . ')';

Regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜