【问题标题】:Angularjs $http service jsonp method keep returning 404 status codeAngularjs $http服务jsonp方法不断返回404状态码
【发布时间】:2019-02-24 10:16:29
【问题描述】:

我遇到了 $http 服务的问题,在使用 jsonp 方法时,我尝试在另一个域上请求一个 html 页面。我得到的实际状态码响应是 200,另一方面 jsonp 方法总是返回 404 状态码。有人以前见过这个问题吗?

经过研究,我发现有人说如果页面内容为空,它将返回状态码 -1 或 404 我不记得确切的响应。

代码:

this.$http
        .jsonp(this.$sce.trustAsResourceUrl('https://target-domain/landing_page'),
    {
                jsonpCallbackParam: 'callback',
            },
            )
        .then((data) => {
            console.log('sucess', data);
        })
        .catch((e) => {
            console.log('error', e);
        });

我正在使用 angularJs 1.6.10

【问题讨论】:

    标签: angularjs jsonp angular-http


    【解决方案1】:

    好的,我找到了这种行为的原因。由于我请求的不是 JSONP 格式的 HTML 页面,因此导致不调用提供的回调。

    下面是负责这个的代码:

        if (event) {
    
        if (event.type === 'load' && !callbacks.wasCalled(callbackPath)) {
          event = { type: 'error' };
        }
        text = event.type;
        status = event.type === 'error' ? 404 : 200;
      }
    

    Jquery 以更好的方式处理这个问题,我得到的状态码是 200 而不是 404。

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 2017-07-29
      • 1970-01-01
      • 2016-12-22
      • 2014-03-05
      • 2017-08-10
      • 2013-06-08
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多