我正在尝试从 Javascript 上的按钮执行 POST RESTful api 调用。 api调用是跨域的,所以这是一个挑战。我如何调用此电话?
在ajax中我的调用如下所示:[我现在知道你不能从ajax进行跨域调用]:
$.ajax({
type: 'POST',
dataType: 'application/json',
accept: 'application/json',
async: false,
username: 'user',
password: 'name',
url: 'http://exterenal.website',
data: {
"name": "Marcus0.7",
"start": 500000,
"end": 1361640526000
},
success: function(){alert('DONE!');},
error:function(){alert('ERROR!')},
});
在 python 中,调用如下所示:
r = requests.post('http://externenal.website' ,headers={'content-type': 'application/json'}, auth=auth, data=json.dumps(data))
谢谢
最佳答案
您需要使用JSONP不是 json。问题是 xmlhttprequest 有一个策略,规定您不能在当前站点域之外发出请求。 JSONP 是解决这个问题的一个技巧。
请注意,您正在违反安全政策。
关于javascript - 来自 Javascript/Django 的 POST RESTful api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18112795/