0

私は過去1時間XMLHttpRequestについて読んでいましたが、私はこれを機能させることができます..だから、私は別のサーバー/ポートにtastypieとJavaScriptクライアントを備えたdjangoサーバーを持っていて、jquery投稿をしようとすると私は入手する

XMLHttpRequest cannot load http://127.0.0.1:8000/api/smart/rating/. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

クライアント側で Xmlthhprequest エラー メッセージが表示される

ジャンゴコード:

class RatingResource(ModelResource):
city = fields.ForeignKey(CityResource, 'city')
user = fields.ForeignKey(UserResource, 'user')
class Meta:
    queryset = Rating.objects.all()
    resource_name = 'rating'
    #authentication = BasicAuthentication()
    #authorization = DjangoAuthorization()

localhost:80 から localhost:8000 への jquery 呼び出しの投稿:

$('#star').raty({
  path: "../assets/img/",
  score    : rating,
  click : function(score, evt) {
      window.rate_app = score;
      var url = "http://127.0.0.1:8000/api/smart/rating/";
      //var comment = $('#textarea').val();
      var comment = "teste do php";
      console.log(cityId);  
      $.post(url,{city : '/api/smart/city/'+cityId+'/' ,comment : comment,id:'4',resource_uri:'/api/smart/rating/4/',rating : score, user: '/api/smart/auth/user/2/'},function(data,status){
          if (data=="error")
              console.log("error");
          else
              console.log("success");
      });

  }
});

アップデート:

HTTP/1.0 401 無許可

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"city": "/api/smart/city/35/", "comment": "teste do php", "id": "4", "resource_uri": "/api/smart/rating/4/", "rating": "3","user_id": "/api/smart/auth/user/2/"}' `http://localhost:8000/api/smart/rating/`
HTTP/1.0 401 UNAUTHORIZED
Date: Mon, 08 Apr 2013 10:52:44 GMT
Server: WSGIServer/0.1 Python/2.7.3
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
Content-Type: text/html; charset=utf-8
Access-Control-Allow-Headers: Content-Type,*
Access-Control-Allow-Credentials: true

私は何を間違っていますか?

4

1 に答える 1

1

こんにちは、基本的にクロスオリジンリクエストを行っていると思います。

クロスオリジン リクエストを開始するために、ブラウザは Origin HTTP ヘッダーを使用してリクエストを送信します。このヘッダーの値は、ページを提供したサイトです。

リクエストを許可するために、レスポンスで Access-Control-Allow-Origin ヘッダーを送信します。ヘッダーの値は、どのオリジン サイトが許可されているかを示します。

Access-Control-Allow-Origin: localhost:80

基本的に、サーバー側で Access-Control-Allow-Origin ヘッダーを返すオプション リクエストを許可する必要があります。

より良いアイデアのためにこれを読んでください

Cross-Origin Resource Sharing を使用したクロスドメイン POST クエリでデータが返されない

于 2013-04-05T18:20:35.533 に答える