2017-03-20 10 views
0

私はPhantomJS内で$ .getJSONを使用しようとしていますが、その結果を得ることは不可能です。どんな解決策ですか?私は単純に直接読み込むことはできません。ページは同じドメインから呼び出される必要があります。PhantomJS getJSONが応答を取得できません

私はページを開いてそこから電話したいと思います。任意の助け

var jqueryUrl = "https://code.jquery.com/jquery-latest.min.js"; 

page.open("http://www.example.com/", function(status) { 
    if (status === "success") { 
      page.includeJs(jqueryUrl, function() { 
      var result = page.evaluate(function() { 
       $.getJSON('http://www.example.com/someJson', function(data) { 
        return data; 
       }); 
      }); 
      console.log(result); 
      phantom.exit(); 
     }); 
    } else { 
     phantom.exit(1); 
    } 
}); 

ありがとう:ここ

が動作していない私の現在のコードです!

+0

-error.html)ハンドラを使用してエラーをチェックします。 – Vaviloff

答えて

1

window.callPhantomとの組み合わせでpage.onCallbackを使用する必要があります。これは、phantomjsコンテキストでHTTP要求を行い、結果が要求の完了後に返される必要があるためです。

私はまさにこのコードをテストしていませんが、それはこのようなものでなければなりません:[page.onError](http://phantomjs.org/api/webpage/handler/onを追加してください

var jqueryUrl = "https://code.jquery.com/jquery-latest.min.js"; 

page.open("http://www.example.com/", function(status) { 
    if (status === "success") { 
     page.onCallback(function(data) { 
      // got the data! 
      console.log(data); 
      phantom.exit(); 
     }); 
     page.includeJs(jqueryUrl, function() { 
      page.evaluate(function() { 
       $.getJSON('http://www.example.com/someJson', window.callPhantom); 
      }); 
     }); 
    } else { 
     phantom.exit(1); 
    } 
}); 
関連する問題