$ http JSONP请求在Chrome / Firefox上获得404,在IE中正常运行

时间:2016-05-26 18:10:19

标签: javascript angularjs jsonp

我正在使用random word API开发一个简单的Hangman应用程序。作者说它与JSONP兼容。

请参阅此小提琴:https://jsfiddle.net/1t8ur23p/7/

相关代码在这里。这在IE中正常工作,我每次都会得到一个随机单词。但我在Firefox和Chrome中获得了404,我无法分辨原因。

function getSecretWord() {
  //call the random word API using JSONP request
  $http.jsonp(
      'http://randomword.setgetgo.com/get.php?callback=JSON_CALLBACK'
  ).then(function(response) {
      console.log(response);
      //apply the random word to the local secret word
      $scope.word = response.data.Word;
      //setup the other parts of the game
      $scope.letters = response.data.Word.split("");
      $scope.answerArray = response.data.Word.replace(/./g, '-')
          .split("");
  }).catch(function(response) {

      //debugging... see the contents of the faulty response
      $scope.error = response;

      //there's been an error, just default the word to 'hello'
      $scope.word = 'hello';
      $scope.letters = $scope.word.split("");
      $scope.answerArray = $scope.word.replace(/./g, '-').split(
          "");
  });
}

1 个答案:

答案 0 :(得分:0)

您是否在HTTPS页面上运行代码?因为这似乎是mixed content page问题。我在Chrome和Firefox上看到以下错误:

Mixed Content: The page at 'https://fiddle.jshell.net/1t8ur23p/7/show/' was loaded over HTTPS, but requested an insecure script 'http://randomword.setgetgo.com/get.php?callback=angular.callbacks._0'. This request has been blocked; the content must be served over HTTPS.

当您尝试从HTTPS页面加载HTTP页面时会发生这种情况。你可以看到,如果你从http://jsfiddle.net/1t8ur23p/7/(http,而不是https)运行代码,它可以正常工作。