Cross-domain $http request AngularJS

后端 未结 2 1309
粉色の甜心
粉色の甜心 2020-12-06 17:40

I have simple website I\'m building with AngularJS which calls an API for json data.

However I am getting Cross domain origin problem is there anyway around this to

相关标签:
2条回答
  • 2020-12-06 17:48

    It seems that api.nestoria.co.uk does not allow CORS. It has to set the Access-Control-Allow-Origin header itself -- you have no direct control over that.

    However, you can use JSONP. That site allows it via the callback query parameter.

    $http.jsonp(baseurl+'country=uk&pretty=1&action=search_listings&place_name=london'
        +encoding+type + "&callback=JSON_CALLBACK")
    
    0 讨论(0)
  • 2020-12-06 17:48

    Install Fiddler. Add a custom rule to it:

    static function OnBeforeResponse(oSession: Session)
    {
        oSession.oResponse.headers.Add("Access-Control-Allow-Origin", "*");
    }
    

    This would allow you to make cross domain requests from localhost. If the API is HTTPS make sure you enable 'decrypt HTTPS traffic' in fiddler.

    Reference

    -------------------- UPDATE

    The response you are getting is JSON. Specifying JSONP as datatype would not work. When you do specify JSONP the return should be a function not JSON object.

    JSONP

    0 讨论(0)
提交回复
热议问题