JSONP请求在Angular应用程序中提供404

rad*_*von 8 javascript jsonp angularjs

我正在尝试从带有JSONP请求的API获取一些数据,但每次都得到404.我认为我的网址有误,但我可以在Chrome中手动点击网址并获得所需的响应.我的$http.jsonp请求总是出错,console.log在错误回调中出现了404 .

这是发出请求的代码:

$scope.fetchDefaultLabel = function () {
      // This code will eventually set the label to the site name when the endpoint field blurs.
      // Currently not working properly.
      if ($scope.endpoint) {
        var baseUrl = $scope.endpoint.split('/')[2];
        var siteNameUrl = 'http://' + baseUrl + '/api/sitename.json?callback=JSON_CALLBACK';
        console.log(siteNameUrl);
        $http.jsonp(siteNameUrl)
          .success(function(data, status, headers, config) {
            $scope.label = data;
          })
          .error(function(data, status, headers, config) {
            console.log('Data: ' + data);
            console.log('Status: ' + status);
            console.log('Headers: ' + headers);
            console.log('Config: ' + config);
          });
      }
    };
Run Code Online (Sandbox Code Playgroud)

Chrome Dev Tools的网络面板显示了对我所期望的响应主体的请求的200响应,但是由于某种原因,它没有进入Angular.

rad*_*von 5

在与一些后端开发人员交谈后,看来我使用的API不支持JSONP.这就是我在该请求上获得404的原因.

  • 这对于角度处理这种错误来说真是一种可怕的方式.给404是令人困惑的.怎么样只是说,"嘿,没有有效的功能来调用脚本"我们都会以我们的快乐方式确保响应对jsonp有效 (2认同)