1

こんにちは、このコードを使用してjsonファイルからコンテンツを取得していますが、結果を表示できません

私のコードは

$(document).ready(function(){

$.ajax({
    url: 'http://50.116.19.49/rest/user.json',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 50000,
    success: function(data, status){
    alert(data);    
    },
    error: function(){
        output.text('There was an error loading the data.');
    }
});
});

Google Chrome では、リクエスト ステータスが 200 で、データがロードされていることがわかります。ここに私がコードをコピーしているリンクがありますリンク....どんな助けでも!!!!

ありがとう

4

3 に答える 3

2

http://50.116.19.49/rest/user.jsonリソースが JSONP ではなく JSON 応答を返すため ( difference ) 、コードは機能しません。

そのため、ajax を介してクロスドメイン呼び出しを行うことはできませんが、サーバー上にプロキシ スクリプトを作成することはできます。例:

クロスドメイン call.php

<?php echo file_get_contents('http://50.116.19.49/rest/user.json');?>

そして、スクリプトを次のように変更します

$(document).ready(function(){

$.ajax({
    url: 'http://127.0.0.1/cross-domain-call.php',
    dataType: 'json',
    timeout: 50000,
    success: function(data, status){
        console.log(data);    
    },
    error: function(jqXHR, textStatus, errorThrown){
        console.log(jqXHR);    
        console.log(textStatus);    
        console.log(errorThrown);    
    }
});
});
于 2012-10-21T17:24:40.673 に答える
0

リモート Web サービスにアクセスして JSONP に変換できないように見えるため、Yahoo の YQL をプロキシとして使用するソリューションを次に示します。YQL は任意のリモート サイトからファイルの内容を取得し、xml または jsonp として返します。

ワーキングデモ http://jsfiddle.net/Uh8cz/2/

応答オブジェクトには、YQL によって作成された追加のプロパティがあることに気付くでしょう。

var yqlBaseUrl = 'http://query.yahooapis.com/v1/public/yql?q=';


var restUrl = 'http://50.116.19.49/rest/user.json';
var yqlQuery = 'select * from json where url="' + restUrl + '" ';
var yqlFormat = 'format=json'
var jQueryCallback = '?'
var fullQueryUrl = yqlBaseUrl + yqlQuery + '&' + yqlFormat + '&' + jQueryCallback;

$.getJSON(fullQueryUrl, function(json) {
    var jsonString = JSON.stringify(json.query.results.json.json, null, ' ')
    $('body').append('<h1>Response</h1><pre>' + jsonString)
})  ​
于 2012-10-21T17:51:53.723 に答える
0

「user.json」の内容を制御できる場合は、関数をラップします。

user.json コンテンツ:

execute_jsonp({you_json)

あなたのJavaScript:

$(document).ready(function(){

    $.ajax({
        url: 'http://50.116.19.49/rest/user.json',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 50000,
        success: function(data, status){
            data(); //execute the function that is sent
        },
        error: function(){
            output.text('There was an error loading the data.');
        }
    });

    function execute_jsonp(json){ //recreate the function so you may access the json as a parameter
        alert(json);
    }
});

お役に立てれば ;)

于 2012-10-22T11:45:18.480 に答える