Why does my JavaScript code receive a No Access-Control-Allow-Origin header is present on the requested resource error while Postman does not

0 votes

I'm attempting to use JavaScript to connect to Flask's built-in RESTful API in order to perform authorization. I receive the following problem when I submit the request, though:

XMLHttpRequest cannot load http://myApiUrl/login. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'null' is therefore not allowed access.

I am aware that the remote resource or API must set the header, so why did the Postman Chrome extension make the request successful?

This is the request code:

$.ajax({
      type: 'POST',
      dataType: 'text',
      url: api,
      username: 'user',
      password: 'pass',
      crossDomain: true,
      xhrFields: {
        withCredentials: true,
      },
    })
      .done(function (data) {
        console.log('done');
      })
      .fail(function (xhr, textStatus, errorThrown) {
        alert(xhr.responseText);
        alert(textStatus);
      });
Sep 19 in Java-Script by Tejashwini
• 540 points
36 views

1 answer to this question.

0 votes
If I understand you well, you are sending an XMLHttpRequest to a domain other than the one that hosts your page. Because of security concerns, the browser is blocking it instead of typically allowing a request from the same origin. To make a cross-domain request, you must take a distinct action.

They are not constrained by this policy when you use Postman. Cross-Origin XMLHttpRequest citation:

Regular web pages can transmit and receive data from distant servers using the XMLHttpRequest object, but they are constrained by the same origin policy. There are more options for extensions. If an extension first asks cross-origin rights, it can communicate with external servers that are not part of its origin.
answered Sep 20 by Abhinaya
• 1,160 points

Related Questions In Java-Script

0 votes
1 answer

Error:No Access-Control-Allow-Origin header is present on the requested resource node.js

Hello @kartik, My guess is that you're serving ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,780 points
3,335 views
0 votes
1 answer

Error:Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers

Hello @kartik, Access-Control-Allow-Headers does not allow * as accepted value. Instead of ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,780 points
990 views
0 votes
1 answer

Error:Origin is not allowed by Access-Control-Allow-Origin

Hello @kartik, The easiest way to handle this ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,780 points
3,059 views
0 votes
0 answers

Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?

I'm attempting to provide authorisation using JavaScript by connecting to the RESTful API integrated into Flask.  However, when I make the request, I receive the following error: XMLHttpRequest cannot load http://myApiUrl/login. No 'Access-Control-Allow-Origin' header ...READ MORE

Sep 20 in Java by Nicholas
• 6,240 points
21 views
0 votes
1 answer

CORS in AWS API Gateway + AWS Lambda

You could try downloading the AWS APIG ...READ MORE

answered Oct 9, 2018 in AWS by Priyaj
• 58,080 points
3,134 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,510 points
331 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,780 points
9,348 views
0 votes
1 answer

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

Include servlet-api-3.1.jar in your dependencies. Maven <dependency> <groupId>javax.servlet</groupId> ...READ MORE

answered Sep 20 in Java-Script by Abhinaya
• 1,160 points
32 views
0 votes
1 answer

What does "javascript:void(0)" mean?

After evaluating the provided expression, the void ...READ MORE

answered Sep 20 in Java-Script by Abhinaya
• 1,160 points
21 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP