3

わかりました、私は Backbone.js を初めて使用し、単純なコレクション フェッチを機能させようとしています。

サーバー側では、次の JSON を送信しています。これが応答本文に含まれることを確認しました。

[{"id":1,"user_id":1,"last_name":"Smith","first_name":"Bob","date_of_birth":"1980-01-06T05:00:00Z","created_at":"2013-04-26T00:50:00Z","updated_at":"2013-04-26T00:50:00Z"},{"id":2,"user_id":1,"last_name":"Smith","first_name":"Roger","date_of_birth":"1985-01-01T05:00:00Z","created_at":"2013-04-27T04:00:00Z","updated_at":"2013-04-27T04:00:00Z"}]

これは JSONLint を渡します

サーバーは Jetty (Scala/Scalatra) の localhost:8080 で実行されており、クライアント側のものは localhost:8083 で実行されている nginx から提供されています。CORSサポートが正しく機能していると思います。いずれにせよ、JSON と共に 200 応答を受け取ります。

リクエストは次のようになります (Opera 開発コンソール):

GET /api/persons HTTP/1.1
User-Agent: Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.15
Host: localhost:8080
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate
Referer: http://localhost:8083/
Connection: Keep-Alive
Accept: */*
X-Requested-With: XMLHttpRequest
Origin: http://localhost:8083

応答のヘッダーは次のとおりです。

HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://localhost:8083
Access-Control-Allow-Credentials: true
Content-Type: application/json;charset=UTF-8
Server: Jetty(8.1.8.v20121106)

これは、フェッチのエラー コールバックをトリップしていました。そこで、次に通常の jquery $.getJSON() を書き、次に同じ req を $.ajax 形式で書きました。運がない。

最後に、$.ajaxSetup にいくつかのチェックを追加しました (ああ、coffeescript、ところで):

$.ajaxSetup 
  error: (jqXHR, exception) ->
    if jqXHR.status == 0
      alert('Not connect.\n Verify Network.')
    else if jqXHR.status == 404
      alert('Requested page not found. [404]')
    else if jqXHR.status == 500
      alert('Internal Server Error [500].')
    else if exception == 'parsererror'
      alert('Requested JSON parse failed.')
    else if exception == 'timeout'
      alert('Time out error.')
    else if exception == 'abort'
      alert('Ajax request aborted.')
    else
      alert('Uncaught Error.\n' + jqXHR.responseText)

「parsererror」がトリガーされています。それで、ね。明らかな何かが欠けています、私はそれを知っています。

4

1 に答える 1

2

設定を使用して、jQuery でも CORS が有効にxhrField.withCredentialsなっていることを確認する必要があります。

于 2013-05-04T15:52:22.147 に答える