I'm trying to use the model.save() with a rest service on another subdomain. λ€λ₯Έ νμ λλ©μΈμ λλ¨Έμ§ μλΉμ€μ ν¨κ» model.save()λ₯Ό μ¬μ©νλ €κ³ ν©λλ€. I got the following request headers which are far from my expectations: λ΄ μμκ³Όλ κ±°λ¦¬κ° λ¨Ό λ€μκ³Ό κ°μ μμ² ν€λκ° μμ΅λλ€.
OPTIONS /user HTTP/1.1
Host: not.public
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://not.public
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
How to fix this? μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ λ°©λ²μ 무μμ λκΉ?
Ofc. μ¬λ¬΄μ€ my REST server responds 404 not found for an OPTIONS call... λ΄ REST μλ²κ° OPTIONS νΈμΆμ λν΄ 404λ₯Ό μ°Ύμ μ μλ€κ³ μλ΅ν©λλ€...
To avoid unwanted questions: the server is ok, it handles REST calls and CORS calls well, tested with $.ajax and a REST interface tester application too. μμΉ μλ μ§λ¬Έμ νΌνκΈ° μν΄: μλ²λ μ μμ΄λ©° REST νΈμΆ λ° CORS νΈμΆμ μ μ²λ¦¬νλ©° $.ajax λ° REST μΈν°νμ΄μ€ ν μ€ν° μ ν리μΌμ΄μ μΌλ‘λ ν μ€νΈλμμ΅λλ€.
enLook up how CORS works -- this is the expected "preflight" request CORS μλ λ°©μ μ°Ύμ보기 -- μμλλ "μ€ν μ " μμ²μ λλ€.
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Preflighted_requests https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Preflighted_requests
enμ΄ λΉν μμ²μ μ΄λ»κ² νΌν μ μμ΅λκΉ?
enhttps://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Simple_requests μ μ μλ λλ‘ "λ¨μν μμ²"λ§ λ³΄λ΄μμμ€.
enI tried this on server side to handle preflight calls: μ€ν μ νΈμΆμ μ²λ¦¬νκΈ° μν΄ μλ² μΈ‘μμ μ΄κ²μ μλνμ΅λλ€.
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
but did not work, the browser does not send another request after the preflight... νμ§λ§ μλνμ§ μμκ³ , λΈλΌμ°μ λ ν리νλΌμ΄νΈ ν λ€λ₯Έ μμ²μ 보λ΄μ§ μμ΅λλ€...
So you say that it is not possible with backbone.sync? κ·Έλμ backbone.syncλ‘λ λΆκ°λ₯νλ€λ λ§μμ΄μ κ°μ?
Then it is a bug I think... κ·Έλ λ€λ©΄ λ²κ·ΈλΌκ³ μκ°ν©λλ€...
κ·ΈλΌμλ λΆκ΅¬νκ³ μ΄κ²μ λ°±λ³Έ λ¬Έμ κ° μλλ―λ‘ Stackoverflow λλ IRCμ μ§λ¬Ένλ κ²μ΄ μ’μ΅λλ€.
enμ΄κ²μ΄ μ λ°±λ³Έ λ¬Έμ κ° μλμ§ μ€λͺ ν΄ μ£Όμκ² μ΅λκΉ?
enBackbone.syncμ μμ€( http://backbonejs.org/docs/backbone.html#section-134 )λ₯Ό μ½μΌλ©΄ Backboneμ΄ λͺ¨λ κ²μ $.ajaxλ‘ μ λ¬νλ κ²μ λ³Ό μ μμ΅λλ€.
en@inf3rno try this one. @inf3rno μ΄κ²μ μλνμμμ€.
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: POST');
This isn't Backbone issue at all. μ΄κ²μ μ ν λ°±λ³Έ λ¬Έμ κ° μλλλ€.
enThese are my original headers without php: λ€μμ PHPκ° μλ μλ ν€λμ λλ€.
HTTP/1.1 200 OK
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/html
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.8, ASP.NET
access-control-allow-origin: http://x.y.loc
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Date: Fri, 17 May 2013 01:33:02 GMT
Content-Length: 0
I think my server is well configured, but I tried out the headers you gave, and they did not work... μ μλ²κ° μ ꡬμ±λμ΄ μλ κ² κ°μλ° μλ €μ£Όμ ν€λλ₯Ό ν΄λ΄€λλ° μλλ€μ...
Simple $.ajax calls work well, for example I load json files from the service with this: κ°λ¨ν $.ajax νΈμΆμ μ μλν©λλ€. μλ₯Ό λ€μ΄ λ€μκ³Ό κ°μ΄ μλΉμ€μμ json νμΌμ λ‘λν©λλ€.
define(function (require, exports, module) {
var $ = require("jquery");
module.exports = {
load: function (name, _require, load, config) {
var params = name.split("|");
var method, url;
if (params.length == 1) {
method = "GET";
url = params[0];
}
else {
method = params[0];
url = params[1];
}
$.ajax({
url: url,
type: method,
dataType: "json"
}).success(load).error(function (xhr, status, error) {
throw error;
});
}
};
});
I tried out this with any request method, and they responded well... λλ μ΄κ²μ μ΄λ€ μμ² λ°©λ²μΌλ‘ μλνκ³ κ·Έλ€μ μ μλ΅νμ΅λλ€ ...
The collection.fetch() works well too, I have problems only with the model.save() ... collection.fetch()λ μ μλν©λλ€. model.save()μλ§ λ¬Έμ κ° μμ΅λλ€ ...
I tried it this way, maybe I'm doing something wrong: λλ μ΄κ²μ μ΄λ° μμΌλ‘ μλνλλ°, μλ§λ λ΄κ° μλͺ»νκ³ μλ κ²μΌ μ μλ€.
var User = Backbone.Model.extend({
urlRoot: Application.service.Authorization.User.create.url
});
var form = new UserForm({
model: new User({
email: "[email protected]",
name: "test"
})
}).render();
form.on("submit", function (user) {
console.log(user.attributes);
user.save(null, {
success: function (model, response, options) {
console.log("success");
},
error: function (model, xhr, options) {
console.log("error");
}
});
});
I got the attributes and "error" in console. μ½μμ μμ±κ³Ό "μ€λ₯"κ° μμ΅λλ€. I'll check what kind of error is.. μ΄λ€ μ€λ₯μΈμ§ νμΈνκ² μ΅λλ€..
enYou were right, thanks! λΉμ μ΄ μ³μμ΅λλ€. κ°μ¬ν©λλ€! I added to server config the allow content-type, and it works now. μλ² κ΅¬μ±μ μ½ν μΈ μ ν νμ©μ μΆκ°νλλ° μ΄μ μλν©λλ€. Firebug is not the best, the Chrome console printed that the content-type is not allowed... Firebugκ° μ΅κ³ κ° μλλ©° Chrome μ½μμμ μ½ν μΈ μ νμ΄ νμ©λμ§ μλλ€κ³ μΈμνμ΅λλ€...
enMany tanks! λ§μ ν±ν¬! :D :λ
enGET
and POST
requests requires quite different headers. GET
λ° POST
μμ²μλ μλΉν λ€λ₯Έ ν€λκ° νμν©λλ€. When the API server uses cookies
for authentication, the server should have Access-Control-Allow-Credentials: true
header. API μλ²κ° μΈμ¦μ μν΄ cookies
λ₯Ό μ¬μ©νλ κ²½μ° μλ²μλ Access-Control-Allow-Credentials: true
ν€λκ° μμ΄μΌ ν©λλ€. And off course you need to make a XHR request with withCredentials: true
on client-side. λ¬Όλ‘ ν΄λΌμ΄μΈνΈ μΈ‘μμ withCredentials: true
λ‘ XHR μμ²μ ν΄μΌ ν©λλ€. When the API server uses Authorization
header for BasicAuth request, you don't need withCredential
. API μλ²κ° BasicAuth μμ²μ Authorization
ν€λλ₯Ό μ¬μ©νλ κ²½μ° withCredential
νμνμ§ μμ΅λλ€. Instead, the Access-Control-Allow-Headers
need to include Authorization
in this case, just like λμ Access-Control-Allow-Headers
Authorization
λ₯Ό ν¬ν¨ν΄μΌ ν©λλ€.
header('Access-Control-Allow-Headers: Authorization, Content-Type');
Ah, it seems you solved the issue while I'm writing this. μ, μ΄ κΈμ μ°λ λμ λ¬Έμ λ₯Ό ν΄κ²°νμ κ² κ°μ΅λλ€.
enλ€, μ΄μ¨λ κ°μ¬ν©λλ€. λ§μ λμμ΄ λμμ΅λλ€!
en@inf3rno Thanks so much for the comment about Chrome giving better error messages than Firebug! @inf3rno Firebugλ³΄λ€ λ λμ μ€λ₯ λ©μμ§λ₯Ό μ 곡νλ Chromeμ λν μ견μ 보λ΄μ£Όμ μ κ°μ¬ν©λλ€! That helped us solve the problem. λ¬Έμ λ₯Ό ν΄κ²°νλ λ° λμμ΄ λμμ΅λλ€.
enYou're welcome! μ²λ§μμ! :-) :-)
enCan someone please help on this issue? λκ΅°κ°μ΄ λ¬Έμ μ λν΄ λμμ μ€ μ μμ΅λκΉ? http://stackoverflow.com/questions/32725249/cors-filter-issue-for-authentrypoint http://stackoverflow.com/questions/32725249/cors-filter-issue-for-authentrypoint
enλ€λ₯Έ μ¬λμ΄ μ΄ λ¬Έμ λ₯Ό κ²ͺκ³ μλ κ²½μ°λ₯Ό λλΉνμ¬ Backbone.emulateHTTP = trueλ‘ μ€μ ν λ μ¬μ©λλ Access-Control-Allow-Method λͺ©λ‘μ "x-http-method-override"λ₯Ό μΆκ°ν΄μΌ νμ΅λλ€.
en
κ°μ₯ μ μ©ν λκΈ
@inf3rno try this one. @inf3rno μ΄κ²μ μλνμμμ€.
This isn't Backbone issue at all. μ΄κ²μ μ ν λ°±λ³Έ λ¬Έμ κ° μλλλ€.
en