【问题标题】:POST RESTful api call from Javascript/Django来自 Javascript/Django 的 POST RESTful api 调用
【发布时间】:2013-08-09 09:14:22
【问题描述】:

我正在尝试通过 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))

谢谢

【问题讨论】:

    标签: javascript jquery python django api


    【解决方案1】:

    您需要使用JSONP 而不是json。问题是 xmlhttprequest 有一个政策规定您不能在当前站点域之外发出请求。 JSONP 是解决这个问题的一个技巧。

    请注意您违反了安全政策。

    Here's a So post that explains how to do this using JQuery.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多