I have a rest server running (python bottle) on the same host that is running my django application. From my django application I am doing a get request using ajax, but the request is failing even though I can see the status of the request succeeding on my rest server(returns code 200, success)
我在运行我的django应用程序的同一主机上运行了一个休息服务器(python bottle)。从我的django应用程序我正在使用ajax执行get请求,但请求失败,即使我可以在我的休息服务器上看到请求的状态成功(返回代码200,成功)
My Ajax code:
我的Ajax代码:
$.ajax({
url: 'http://hostname:port/ListVms/',
crossDomain: true,
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
alert('worked');
},
error: function() {
alert("Sorry, error retrieving!");
}
});
Python Rest method:
Python Rest方法:
@route('/ListVms/', method='GET')
def get_event():
return json.dumps(JS)
Output from the rest service:
休息服务的输出:
[22/Aug/2012 10:25:45] "GET /ListVms/?callback=jQuery17102901058990901989_1345656355311&_=1345656355356 HTTP/1.1" 200 44
1 个解决方案
#1
5
I'm guessing your server is returning JSON, like this:
我猜你的服务器正在返回JSON,如下所示:
{ "abc": 123 }
This format is incompatible with JSONP. JSONP requires a callback function that the JSON is passed to. The callback function is passed to your server in that query string.
此格式与JSONP不兼容。 JSONP需要一个传递JSON的回调函数。回调函数将在该查询字符串中传递给您的服务器。
So using JSONP the browser plunks down a script tag like this:
因此,使用JSONP,浏览器会像这样下载一个脚本标记:
<script src="http://hostname:port/ListVms/?callback=jQuery17102901058990901989_1345656355311"></srcipt>
And your server should respond with this:
你的服务器应该回复:
jQuery17102901058990901989_1345656355311({
"abc": 123
});
Typically servers that serve JSON and want to support JSONP have a conditional that looks kind of like this:
通常,服务于JSON并且想要支持JSONP的服务器具有看起来像这样的条件:
if queryString.callback
render queryString.callback + "(" + jsonData + ")"
else
render jsonData
#1
5
I'm guessing your server is returning JSON, like this:
我猜你的服务器正在返回JSON,如下所示:
{ "abc": 123 }
This format is incompatible with JSONP. JSONP requires a callback function that the JSON is passed to. The callback function is passed to your server in that query string.
此格式与JSONP不兼容。 JSONP需要一个传递JSON的回调函数。回调函数将在该查询字符串中传递给您的服务器。
So using JSONP the browser plunks down a script tag like this:
因此,使用JSONP,浏览器会像这样下载一个脚本标记:
<script src="http://hostname:port/ListVms/?callback=jQuery17102901058990901989_1345656355311"></srcipt>
And your server should respond with this:
你的服务器应该回复:
jQuery17102901058990901989_1345656355311({
"abc": 123
});
Typically servers that serve JSON and want to support JSONP have a conditional that looks kind of like this:
通常,服务于JSON并且想要支持JSONP的服务器具有看起来像这样的条件:
if queryString.callback
render queryString.callback + "(" + jsonData + ")"
else
render jsonData