Backbone: CORS λ™κΈ°ν™”λŠ” POST λŒ€μ‹  OPTIONS μš”μ²­μ„ λ³΄λƒ…λ‹ˆλ‹€.

에 λ§Œλ“  2013λ…„ 05μ›” 17일  Β·  17μ½”λ©˜νŠΈ  Β·  좜처: jashkenas/backbone

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 μΈν„°νŽ˜μ΄μŠ€ ν…ŒμŠ€ν„° μ• ν”Œλ¦¬μΌ€μ΄μ…˜μœΌλ‘œλ„ ν…ŒμŠ€νŠΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

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. 이것은 μ „ν˜€ λ°±λ³Έ λ¬Έμ œκ°€ μ•„λ‹™λ‹ˆλ‹€.

en

λͺ¨λ“  17 λŒ“κΈ€

Look 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

이 λΉ„ν–‰ μš”μ²­μ„ μ–΄λ–»κ²Œ ν”Όν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

en

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Simple_requests 에 μ •μ˜λœ λŒ€λ‘œ "λ‹¨μˆœν•œ μš”μ²­"만 λ³΄λ‚΄μ‹­μ‹œμ˜€.

en

I 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... κ·Έλ ‡λ‹€λ©΄ 버그라고 μƒκ°ν•©λ‹ˆλ‹€...

en

κ·ΈλŸΌμ—λ„ λΆˆκ΅¬ν•˜κ³  이것은 λ°±λ³Έ λ¬Έμ œκ°€ μ•„λ‹ˆλ―€λ‘œ Stackoverflow λ˜λŠ” IRC에 μ§ˆλ¬Έν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

en

이것이 μ™œ λ°±λ³Έ λ¬Έμ œκ°€ μ•„λ‹Œμ§€ μ„€λͺ…ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ?

en

Backbone.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. 이것은 μ „ν˜€ λ°±λ³Έ λ¬Έμ œκ°€ μ•„λ‹™λ‹ˆλ‹€.

en

These 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.. μ–΄λ–€ 였λ₯˜μΈμ§€ ν™•μΈν•˜κ² μŠ΅λ‹ˆλ‹€..

en

You 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 μ½˜μ†”μ—μ„œ μ½˜ν…μΈ  μœ ν˜•μ΄ ν—ˆμš©λ˜μ§€ μ•ŠλŠ”λ‹€κ³  μΈμ‡„ν–ˆμŠ΅λ‹ˆλ‹€...

en

Many tanks! λ§Žμ€ 탱크! :D :λ””

en

GET 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. 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 데 도움이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

en

You're welcome! μ²œλ§Œμ—μš”! :-) :-)

en

Can 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
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰