1

I am developing an HTML page where I want to get the prices of the products sold on Flipkart.

I have done following code to get items:

$(document).ready(function(){
     $.ajax({
       url: "https://affiliate-api.flipkart.net/affiliate/search/json?query=sony+mobiles&resultCount=5",
       type: "GET",
       headers:{"Accept" : "application/json;odata=verbose", "Fk-Affiliate-Id" : "[ID]", "Fk-Affiliate-Token": "[Token]","Access-Control-Allow-Origin" : "*"},
       success: function(){console.log("Success");},
       error: function(){console.log("Fails");}
    });
});

But the execution always goes to error part. If I run the same query in the REST API tool i.e. Yet another rest client (a chrome tool), I am getting the response. What am I missing here?

Mohemmad K
  • 809
  • 7
  • 33
  • 74
  • What is the error that you get, try with `error: function(jqXHR) { console.log(jqXHR) }` to see the error. – Shubham Khatri Nov 29 '16 at 03:52
  • I ran the same code in another machine and it's working. Will there be any issues with that? The error occurred in Windows 8.1 where the error is not appearing Windows 7. – Mohemmad K Nov 29 '16 at 05:12
  • What is the error that you are getting? – Abhishek Dhanraj Shahdeo Nov 29 '16 at 05:15
  • The error in console shows: `Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.` – Mohemmad K Nov 29 '16 at 05:56
  • I also appended `"Access-Control-Allow-Origin" : "*"` in header but still not working. @AbhishekDhanrajShahdeo – Mohemmad K Nov 29 '16 at 06:03
  • How did you append in the header? – Abhishek Dhanraj Shahdeo Nov 29 '16 at 06:29
  • I have changed the code in question. I have made 4 headers, previously they were 3. – Mohemmad K Nov 29 '16 at 06:31
  • I don't see your code running in my `linux` machine too. I think you must check on the server side that the proper permissions are provided for you application. – Abhishek Dhanraj Shahdeo Nov 29 '16 at 06:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129318/discussion-between-mohemmad-k-and-abhishek-dhanraj-shahdeo). – Mohemmad K Nov 29 '16 at 06:50
  • Exactly the same issue I am facing, I am able to get response in Postman, but not in chrome console or even jsfiddle. I can't share jsfiddle link as it has token value in it. Please let me know if you have found any solution? @MohemmadK – foodiepanda Oct 21 '20 at 03:57
  • @MohemmadK Did u succeed in fetching the API response? I'm also stuck at same point. – Neel Dsouza Feb 28 '21 at 07:39

0 Answers0