【问题标题】:$http({method}) not working but $http.get working$http({method}) 不工作,但 $http.get 工作
【发布时间】:2015-09-12 22:41:12
【问题描述】:

问题陈述: 在我的项目中,我使用 $http({ 方法: 'GET', url : data : ..... 参数,对于 getPost 它工作正常。但 当我在小提琴中使用相同的方法时,它会阻止我的请求。如果我 使用 $http.get(.... 那么它将在 jsfiddle 中工作不知道为什么

谁能解释一下 $http.get('http://...$http({mthod :'GET',....})

看看这两个例子:


1) 示例:https://jsfiddle.net/kevalbhatt18/84e02j4t/8/

var promise = $http({
            method : 'GET',
            url : 'http://d.yimg.com/autoc.finance.yahoo.com/autoc',
            data : entity,
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'YAHOO.Finance.SymbolSuggest.ssCallback',
            headers : {
                'Content-Type' : 'application/json'
            },
            cache : false
        }).then(function (response) {
            return response;
        });

错误:在第一个示例中,在控制台中查看它会给您错误。

XMLHttpRequest cannot load https://autoc.finance.yahoo.com/autoc. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fiddle.jshell.net' is therefore not allowed access.

2) 使用 $http.get(... 示例:http://jsfiddle.net/kevalbhatt18/qny7v23f/

 test: function (param) {

            $http.get('http://d.yimg.com/autoc.finance.yahoo.com/autoc', {
                params: {
                    query: param,
                    callback: 'YAHOO.Finance.SymbolSuggest.ssCallback'
                }
            })
                .success(function (data, status, headers, config) {
                return data;
            })
                .error(function (data, status, headers, config) {
                return "there was an error";
            })
        }
    }

我在 chrome 中使用 Cors 应用,所以它会添加 Access-Control-Allow-Origin'

【问题讨论】:

  • 您是否有权通过 JSFiddle 使用该 URL?两者都对我不起作用。另请注意,您在这两种情况下使用的 URL 是不同的。
  • 我正在使用 Cors 应用程序,因此它适用于第二个应用程序,但同样适用于 1) 示例

标签: javascript jquery ajax angularjs xmlhttprequest


【解决方案1】:

CORS 标头必须出现在您请求的远程站点上。不是你的

【讨论】:

    猜你喜欢
    • 2012-03-20
    • 2018-03-16
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多