javascript - $http({method}) 不工作,但 $http.get 工作

标签 javascript jquery ajax angularjs xmlhttprequest

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

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

查看这两个示例:

<小时/>

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'

最佳答案

CORS header 必须存在于您所请求的远程站点上。不是你的

关于javascript - $http({method}) 不工作,但 $http.get 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31065346/

相关文章:

javascript - J'avaScript 不在 HTML 中

javascript - 为什么 FB.Canvas.getPageInfo() 的值返回全零

javascript - jQuery - 从索引中获取子项作为 jQuery 对象

javascript - 如何更改 TinyMce 编辑器中的默认字体调色板?

java - 从 BufferedReader (request.getInputStream()) 和 request.getParameter 读取 servlet 上的 jsp 数据的区别

javascript - AngularJS 与 ui.router : reload page when clicking the same state link

javascript - 在 View - Backbone 上的元素后面附加多个 View

javascript - 通过传递特定行的列名来查找 html 表格单元格的值

javascript - 函数不返回任何内容(异步?)

php - AJAX 上传不起作用,它会重新加载整个页面