0

I am trying to call api through ajax call by simply doing

$.ajax({
    type: "POST",// GET in place of POST
    url: 'http://api.ecomexpress.in/apiv2/fetch_awb/',        
    data : ({username: "",password: "",count: 2,type: 'COD'}),    
    success: function (result) {      
      window.alert("success!!");
    },
    error: function (xhr, ajaxOptions, thrownError) {       
    }
});

and got the simple json response like {"reference_id": 4231709, "success": "yes", "awb": [851001207, 851001208]}

thats the valid json response what i need, but still browser shows me an error(error function is getting called instead of success function ), i tried test the error but response text shows blank

Anish
  • 558
  • 3
  • 10
  • 33
  • If you check the console to see the actual error you'll find this: `No 'Access-Control-Allow-Origin' header is present on the requested resource` (http://jsfiddle.net/g2p5bjbL/). This means that the domain you're calling does not include CORS headers in the response. Hence the response data is blocked by the Same Origin Policy. Unless the API you're calling can return JSONP data then what you're attempting to do in JS is not possible. You will have to make the request on the server side instead. See the duplicate question for more information – Rory McCrossan Jan 27 '17 at 09:31
  • @RoryMcCrossan you are right but how can i make the request on erver side any idea??? – Anish Jan 27 '17 at 09:35
  • That would depend on whatever server language you use. It's a popular topic so if you Google I'm sure there will be plenty of guides – Rory McCrossan Jan 27 '17 at 09:36

0 Answers0