PHP, jQuery Ajax and json return over a cross domain

為{幸葍}努か 提交于 2019-12-06 05:17:22

JSONP isn't actually JSON. It's a bit of a "hack". JSONP is actually a JavaScript file, that gets downloaded and ran.

In your PHP page, you should be passed a callback parameter. You need to "wrap" your JSON in it. It should look like this:

func({json: data})

So, your PHP should look like this:

echo $_GET['callback'] . '(' . json_encode($v) . ')';
Nicola Peluchetti

I use this usually but maybe there is a better way

<?php header('content-type: application/javascript; charset=utf-8');

$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

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

since i saw you provided a callback parameter everything should be ok

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!