3

実行中のブラウザーを使用してサービスの URL にアクセスし、このようなブラウザーで json の結果を表示するときに問題に直面しています。

  /*-secure-{"statusCode":200,"errors":
  ],"isSuccessful":true,"statusReason":"OK","Envelope":
 {"Body":{"GetStoresByZipcodeResponseElement":
 {"ns1":"http:\/\/abc.com\/intg\/ws\/\/provider","status":"statusCode":"000","statusDescription":"Success"}}*/"

しかし、jqueryを使用してそのURLにアクセスすると:

 $.ajax({
        url: url,
        cache: true,
        dataType: 'script',
        type: 'GET',
        async: false, // must be set to false
        success: function (data, success) {
           console.log(" success "+ JSON.stringify(success));
           console.log(" data " +JSON.stringify(data));
        },
        error :function( jqxhr, textStatus, error ) { 
        var err = textStatus + ', ' + error;
        console.log( "Request Failed: " + err);
        },

        complete: function (jqxhr, textStatus ){
        console.log( "complete: " + JSON.stringify(jqxhr)+" "+ textStatus );
        }
    });

firebugでの応答を見せてくれますが、それはajaxの成功関数になり、データについては未定義と言っています。

「スクリプト」を「json」で変更した後、これを返します:

Request Failed: error,
complete: {"readyState":0,"responseText":"","responseJSON":null,"status":0,"statusText":"error"} error

Firebug の JSON 応答と [応答] タブ ビュー Firebug での JSON 応答 Firebug での応答

どんな助けでも大歓迎です。

4

1 に答える 1

2

これらのコールバック関数を試して、「データ」とは何かを調べてください。

success: function(data, success) {
  console.log("success", arguments);
  console.log("data", typeof data, data); // Verify the response
},
error: function(jqxhr, textStatus, error) { 
  console.log("error", arguments);
},
complete: function(jqxhr, textStatus) {
  console.log("complete", arguments);
}

また、デバッグ中にキャッシュを無効にしないのはなぜですか? cache: false.

応答が有効な JSON ではないため、セキュリティで保護された JSONデータを要求しようとしているようです。これがおそらく jQuery が解析できない理由です。

必要に応じて、カスタムを記述しdataFilterて応答をトリミングおよび解析することができます。

dataFilter: function(data, type) {

  if (type === 'json') {
    // TODO: Parse the custom format by removing the comments
    // and then parsing what is expected to be valid JSON.
    return $.parseJSON(data);
  }

  return data;
}

アップデート

Same Origin Policyによって制限されているため、サービス プロバイダーがサポートしている場合は、クロスドメイン リクエストを作成しようとしています。JSONPを使用する必要があります。

于 2013-06-08T23:12:01.363 に答える