2

私は文字通りインターネットを精査して、AngularJS を使用して自分のデータをサーバーに投稿するための正しいアプローチを見つける方法を探しています。

http://localhost:8000Angular JS で $http POST リクエストを実行すると、データがサーバーにポストされますが、「Origin is not allowed by Access-Control-Allow-Origin」というエラー メッセージが表示されて失敗します。「file: ///"、結果は同じです。

インターネットで言及されているすべての救済策を試しました

 app.config(['$httpProvider', function ($httpProvider) {
            $httpProvider.defaults.useXDomain = true;
            delete $httpProvider.defaults.headers.common['X-Requested-With'];
        }]);

この記事のほとんどの指示に従いまし たが、うまくいきません。

サーバー側のメソッドも設定Access-Control-Allow-Origin: *しましたが、まだ同じエラーが発生しています。以下は私のコードですaccess-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept, access_token, Origin, X-Requested-With, Content-Type, Accept, access_token

var URL = "https://myURL";
        var app = angular.module('myApp', []);
        app.config(['$httpProvider', function ($httpProvider) {
            $httpProvider.defaults.useXDomain = true;
            delete $httpProvider.defaults.headers.common['X-Requested-With'];
        }]);
        app.controller('UserController', function($scope, $http){
            $scope.user = {};
            $scope.createUser = function () {
                $http({
                    url: URL,
                    data: $scope.user,
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json",
                        "access_token": "sometoken"
                    }
                }).success(function(response){
                            $scope.response = response;
                            alert('ok');
                        }).error(function(error){
                            $scope.error = error;
                            alert('error');
                        });
            }
        });

ここで何が間違っているのかわかりません。助けてください。

編集:

以下は私のプリフライトヘッダーです

Request URL:https://myURL
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
:host:someapp.appspot.com
:method:OPTIONS
:path:somepath
:scheme:https
:version:HTTP/1.1
accept:*/*
accept-encoding:gzip,deflate,sdch
accept-language:en-US,en;q=0.8
access-control-request-headers:accept, origin, access_token, content-type
access-control-request-method:POST
dnt:1
origin:http://localhost:8000
referer:http://localhost:8000/samplepost.html
user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Response Headersview source
access-control-allow-headers:Origin, X-Requested-With, Content-Type, Accept, access_token
access-control-allow-origin:*
alternate-protocol:443:quic
content-length:0
content-type:text/html
date:Mon, 16 Sep 2013 17:58:03 GMT
server:Google Frontend
status:200 OK
version:HTTP/1.1

そして、以下は私の POST ヘッダーです:

Request URL:someURL
Request Headersview source
Accept:application/json, text/plain, */*
access_token:dsfdsfds
Content-Type:application/json
Origin:http://localhost:8000
Referer:http://localhost:8000/samplepost.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Request Payloadview source
{name:adasds, email:sdsfd@gmail.com, phone:325325325, comments:fddsfsdfsd}
comments: "fddsfsdfsd"
email: "sdsfd@gmail.com"
name: "adasds"
phone: "325325325"

Firefox のリクエスト ヘッダーとレスポンス ヘッダー:

Response Headersview
Access-Control-Allow-Orig...    *, *
Alternate-Protocol  443:quic
Cache-Control   private
Content-Encoding    gzip
Content-Length  136
Content-Type    application/json
Date    Mon, 16 Sep 2013 18:30:17 GMT
Server  Google Frontend
Vary    Accept-Encoding
X-Firefox-Spdy  3
access-control-allow-head...    Origin, X-Requested-With, Content-Type, Accept, access_token, Origin, X-Requested-With, Content-Type, Accept, access_token
Request Headersview source
Accept  application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control   no-cache
Connection  keep-alive
Content-Length  100
Content-Type    application/json; charset=UTF-8
Host    someapp.appspot.com
Origin  null
Pragma  no-cache
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
access_token    dsfdsfds
4

3 に答える 3

4

同様の問題がありました。オンラインで見ることができるすべての提案を試しましたが、どれもうまくいきませんでした。投稿リクエストを行う前に、 Content-Typetoを手動で設定する必要がありました。application/x-www-form-urlencoded

これは次の方法で実行できます。

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
于 2013-10-08T22:28:04.703 に答える
2

私はこれを解決しました: http から https への Cross-Origin Request を作成しています。

これは仕様には記載されていませんが、問題を無視して chrome でテストできます。

すべて http またはすべて https に固執する必要があります。

幸運を。

于 2014-02-04T01:31:21.200 に答える