骨干js + cors和save()方法 [英] backbonejs + cors and save() method

查看:25
本文介绍了骨干js + cors和save()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行 POST 抛出保存方法.这是我的模型.

I'm trying to execute a POST throw the save method. Here is my model.

app.Models.Dummy = Backbone.Model.extend({
    initialize: function () {
        url = 'http://anotherdomain/Hello/';
    },
});

当我执行时:

    dummy.save({text : "greg"}, {
        success : function(){       
            console.log('Ok!');
        }, 
        error: function(){
            console.log('Error');
        }
    });

使用 OPTIONS 标头(代码 200)触发请求,但从未触发 POST 请求.但是当我执行时:

The request is fired with an OPTIONS header (code 200) but the POST request is never fired. However When i execute:

    $.ajax({
        type: 'POST',
        url: "http://anotherdomain/Hello/",
        data: {text:"greg"},
        success: function(r) { alert(r.Result) },
        dataType: "application/json"
    });

成功了!

我是否需要覆盖主干中的某些内容?

Does i need to override something in backbone?

请求是:

OPTIONS http://anotherdomain/Hello/ HTTP/1.1
Host: anotherdomain
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: http://mydomain
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Pragma: no-cache
Cache-Control: no-cache

响应是:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 0
Server: Microsoft-IIS/7.5
Set-Cookie: ARRAffinity=611c389e4fd6c5d83202b700ce5627f6e0850faf0604f13c25903b4662919f36;Path=/;Domain=anotherdomain
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5
X-Powered-By: ASP.NET
Date: Wed, 05 Dec 2012 18:44:27 GMT

推荐答案

这不是 CORS 的有效 OPTIONS 响应.响应需要包含标头,告诉浏览器什么是允许的.例如(取自 MDN):

That's not a valid OPTIONS response for CORS. The response needs to include headers that tell the browser what's allowed. For example (taken from MDN):

Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: X-PINGOTHER

我知道您说 $.ajax 有效,但基于此 CORS 响应,我怀疑这是否准确并建议您仔细检查.在幕后,主干本身只是使用 $.ajax

I know you said that the $.ajax worked but based on this CORS response I doubt that is accurate and suggest you double check. Under the covers, backbone is itself just using $.ajax

这篇关于骨干js + cors和save()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆