这是两个页面,test.php和testserver.php.
test.php的
<script src="scripts/jq.js" type="text/javascript"></script>
<script>
$(function() {
$.ajax({url:"testserver.php",
success:function() {
alert("Success");
},
error:function() {
alert("Error");
},
dataType:"json",
type:"get"
}
)})
</script>
Run Code Online (Sandbox Code Playgroud)
testserver.php
<?php
$arr = array("element1",
"element2",
array("element31","element32"));
$arr['name'] = "response";
echo json_encode($arr);
?>
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:当这两个文件都在同一台服务器(localhost或web服务器)上时,它可以工作并被alert("Success")调用; 如果它位于不同的服务器上,意味着Web服务器上的testserver.php和localhost上的test.php,它就无法工作,并且alert("Error")正在执行.即使ajax中的URL更改为http://domain.com/path/to/file/testserver.php
有人可以帮我解决如何开始使用JSONP吗?
码:
$('document').ready(function() {
var pm_url = 'http://twitter.com/status';
pm_url += '/user_timeline/stephenfry.json';
pm_url += '?count=10&callback=photos';
var photos = function (data) {
alert(data);
};
$.ajax({
url: pm_url,
dataType: 'jsonp',
jsonpCallback: 'photos',
jsonp: false,
});
});
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/R7EPt/6/
应该产生一个警报,据我可以从文档中解决:不是(但也没有产生任何错误).
谢谢.
我正在使用JS对象来创建使用Google可视化的图形.我正在尝试设计数据源.起初,我创建了一个JS对象客户端.
var JSONObject = {
cols: [{id: 'date', label: 'Date', type: 'date'},
{id: 'soldpencils', label: 'Sold Pencils', type: 'number'},
{id: 'soldpens', label: 'Sold Pens', type: 'number'}],
rows: [{c:[{v: new Date(2008,1,1),f:'2/1/2008'},{v: 30000}, {v: 40645}]},
{c:[{v: new Date(2008,1,2),f:'2/2/2008'},{v: 14045}, {v: 20374}]},
{c:[{v: new Date(2008,1,3),f:'2/3/2008'},{v: 55022}, {v: 50766}]}]
};
var data = new google.visualization.DataTable(JSONObject, 0.5);
Run Code Online (Sandbox Code Playgroud)
现在我需要动态获取数据.所以我发送一个AJAX请求到一个返回JSON字符串的页面:
"cols: [{id: 'date', label: 'Date', type: 'date'},
{id: 'soldpencils', label: 'Sold Pencils', type: 'number'},
{id: 'soldpens', label: 'Sold Pens', type: 'number'}],
rows: [{c:[{v: new Date(2008,1,1),f:'2/1/2008'},{v: 30000}, {v: …Run Code Online (Sandbox Code Playgroud) 我到处都看这个.我只需要一个简单的"操作方法"拉jsonp跨域.我正在使用jQuery 1.5.1.
我在另一个网站上的程序中尝试了以下内容:
$.getJSON("http://www.mydomain.com/testjson.json?jsoncallback=?", function(data) {
alert("I'm hitting this.");
}
Run Code Online (Sandbox Code Playgroud)
这根本不起作用.
有没有办法只做一个简单的跨域jquery JSONP调用?
谢谢
我正在尝试让JSONP给我一个正确的响应并将其传递给我的回调函数jsonp_callback.使用以下代码:如何设置JSONP?
header('content-type: application/json; charset=utf-8');
$data = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo $_GET['jsonpCallback'] . '('.json_encode($data).')';
Run Code Online (Sandbox Code Playgroud)
和
$.ajax({
url: 'jsonp-response.php',
dataType:'jsonp',
jsonp: 'jsonp_callback',
success: function (r){
console.log(r);
}
});
function jsonp_callback (r){
console.log('jsonp_callback:',r);
}
Run Code Online (Sandbox Code Playgroud)
Sofar我收到的响应如下:
jQuery1102035954900085926056_1381230228656({"a":1,"b":2,"c":3,"d":4,"e":5})
Run Code Online (Sandbox Code Playgroud)
看看测试静态jsonp响应的第一个答案,我想我正确地做了但我不确定为什么jQuery给了我一个独特的字符串.
我如何让我的回复看起来像这样?
jsonp_callback({"a":1,"b":2,"c":3,"d":4,"e":5})
Run Code Online (Sandbox Code Playgroud)