6

I am using jQuery ajax to send request to some API. Due to the CORS policy I got a CORS error on the browser's console

Here's by code

$.ajax({
 url: sendHere,//api url
 type: 'GET',
 contentType: 'text/plain',
 crossDomain: true,
 beforeSend: function(xhr){
 xhr.withCredentials = true;
 },
 }).done(function (result) {
 console.log(result);
 }).error(function (err) {
 //console.log(err);
 });

Error

'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.mywebsite.com' is therefore not allowed access.

I tried to solve this problem by installing a chrome extension to enable allow cross origin request. This extension somehow solved my problem and got a response from the api. But installing an extension is not good.

I also tried to make the request with JSONP(dataType:'jsonp') but the response given by the api is not in json format, it is string so it gives an error.

Code with JSONP

$.ajax({
 url: sendHere,//api url
 type: 'GET',
 crossDomain: true,
 dataType:'jsonp',
 }).done(function (result) {
 console.log(result);
 }).error(function (err) {
 //console.log(err);
 });

Uncaught ReferenceError: E0002 is not defined where "E0002" is the response string from the api

!!!PLEASE HELP!!!

aydow
3,8313 gold badges27 silver badges41 bronze badges
asked Nov 10, 2016 at 6:11
10
  • You can follow this link stackoverflow.com/questions/39659559/angular2-cors-issue/… Commented Nov 10, 2016 at 6:14
  • 2
    What happens when you change your console.log(result); statement as console.log(JSON.stringify(result)); Commented Nov 10, 2016 at 6:14
  • @DavidR the error occurs even on console.log(JSON.stringify(result)) also Commented Nov 10, 2016 at 6:22
  • Can you try adding jsonp: 'callback', after your dataType: 'jsonp', statement? Commented Nov 10, 2016 at 6:26
  • 1
    I have removed type: 'GET' and changed the contentType from contentType: 'text/plain', to contentType: 'application/json', and tested the code with a webservice which works well here => jsfiddle.net/9pqhwrpv/2 Commented Nov 10, 2016 at 6:56

2 Answers 2

2

There are 2 situations -

  1. If you have control over the api code then

    make changes in header and add your origin as well.

  2. If you don't have control to change CORS header that is coming from the api

    you have only one option create a backend code(your own api) in any language you prefer that make an http request and get the data. now use your own api to get data on your frontend.

answered Nov 10, 2016 at 12:04
Sign up to request clarification or add additional context in comments.

3 Comments

"thanks" this worked... I am using PHP as my backend code and used it's CURL to send request to the API by which i was successful to catch response thankyou very much once again
@SelvesanMalakar If this works for you, you might like to accept it as correct answer. It might help others to find correct answer for same/similar problem.
Is calling API from Frontend and Backend different?
1

The cors error is cross origin request policy maintained by the browser for security. To solve your problem either you will have to allow cors request in your server coding or if you do not have access to the server api code you will have to make the api call from your server to api server

answered Dec 25, 2017 at 5:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.