1

Problem Statement: In my project I am using $http({ method : 'GET', url : data : ..... param, for get and Post it works fine. But when I use same method in fiddle it will block my request. If I use $http.get(.... then it will work in jsfiddle dont know why

Can any one explain me what is difference between $http.get('http://... and $http({mthod :'GET',....})

See these both example :


1) Example: 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;
        });

ERROR : in first example see in console it will give you error.

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) using $http.get(... Example: 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";
            })
        }
    }

And I am using Cors app in chrome so it will add Access-Control-Allow-Origin'

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Keval Bhatt
  • 6,224
  • 2
  • 24
  • 40
  • Are you authorized to use that URL through JSFiddle? Both don't work at my end. Also note the URLs you are using in both cases are different. – Shaunak D Jun 26 '15 at 05:12
  • http://stackoverflow.com/questions/9310112/why-am-i-seeing-an-origin-is-not-allowed-by-access-control-allow-origin-error – ramamoorthy_villi Jun 26 '15 at 05:14
  • i am using Cors application so it will work for second one but sam thing not working for 1 ) example – Keval Bhatt Jun 26 '15 at 06:51

1 Answers1

0

CORS headers is required to be present on remote site, the one that you're requesting. Not yours

Lilly
  • 28
  • 4