javascript - $http JSONP 请求在 Chrome/Firefox 上导致 404,在 IE 中正常工作

标签 javascript angularjs jsonp

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

看到这个 fiddle :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(
          "");
  });
}

最佳答案

您是否在 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),它工作正常。

关于javascript - $http JSONP 请求在 Chrome/Firefox 上导致 404,在 IE 中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468182/

相关文章:

javascript - 可以通过浏览器访问远程 json 组件,但不能通过 $.getJSON() 以编程方式访问

javascript - 使用 JSONP 加载 html 页面

jquery - 用指针更改 angularjs slider 的颜色

javascript - Angularjs - 在嵌套的 ng-repeaters 中,你如何在嵌套的转发器中获取外部转发器项目的索引

javascript - 在进入下一行之前,await 函数不会执行。 (异步/等待不起作用)

php - 使用 javascript 或 php 删除或清除 cookie

mysql - 服务器 Ajax/http 上的 Nodejs 调用不读取新数据 Mongodb

javascript - Angular 1.6.3 不允许 1.5.8 中允许的 JSONP 请求

javascript - 在 Selenium WebDriver 中执行 '.hover' Javascript

javascript - 使用 G+ 登录的 Chrome 扩展程序 - redirect_uri_mismatch