how Postman send requests? ajax, same origin policy

JavascriptJqueryAjaxApiPostman

Javascript Problem Overview


I have found this very useful Chrome extension called Postman. This is a very useful extension especially when you are into programming RESTful applications.

One thing I am confused on is that how this plugin/extension able to send POST request successfully on different domains?

I tried voting in a poll using Postman like this.

Voting using Postman

After submitting that, the vote was actually counted in, but when I tried doing that using AJAX and JavaScript, it fails, because of different origin policy of browsers.

How is that even possible?

Here is my code using jQuery. I used that in my computer though, localhost.

init: function() {
	$.ajax({
		url: 'http://example.com/vote.php',
		type:'POST',
		dataType: 'html',
		data: {
			id: '1'
		},
		success: function(data) {
		if ( data == 'voted' ) {
			$('.set-result').html( 'you already voted. try again after 24 hours' );
		} else {
			$('.set-result').html( 'successfully voted' );
		}
	}
	});
},

Javascript Solutions


Solution 1 - Javascript

Chrome packaged apps can have cross domain permissions. When you install Postman it promts you that this app will access any domain.

By placing */* in permissions section of your manifest file, you can do this.

Read more here: https://developer.chrome.com/extensions/xhr.html

Solution 2 - Javascript

You can add the following header to sent Ajax request in postman.

Content-Type      application/json

X-Requested-With  XMLHttpRequest

Screenshot

enter image description here

Credit to Orion

Solution 3 - Javascript

Sounds like the site that hosts the poll (the "vote.php" script) needs to have an "Access-Control-Allow-Origin" header set to allow posting from a list of sites (or all sites).

A value of * for the header will allow posting from any website:

Access-Control-Allow-Origin: *

i.e. You could put the following at the top of vote.php

header('Access-Control-Allow-Origin: *');

Chrome extensions and apps are not subject to the same security limitations placed on normal webpages.

Additional debugging tips:

If you're trying to access remote services from web pages you have open on your local file system in your browser, you might find your browser applies different security rules to them than it does to files served from a web service.

e.g. If you open local files from a locational like C:\MyDocuments\weboot\index.htm (Windows) or \Users\joe\Sites\index.html (Mac) in your browser your AJAX request might not work, even with the header specified in most browsers.

Apple's Safari applies almost no cross domain restrictions to files opened locally but Firefox is much more strict about what it permits, with Chrome somewhere in the middle. Running a web server locally (e.g. on http://localhost/) is a good idea to avoid unexpected behaviour.

Additionally, other libraries that provide functions to handle Ajax requests (such as AngularJS) may require other headers to be set on the server by default. You can usually see the reason for failure in a browser debug console.

Solution 4 - Javascript

2021 Oct

In my investigation, I found out that you need an extra field in the header of your request. So simply add the following key-value into the header:

key: X-Requested-With | value: XMLHttpRequest

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoey HipolitoView Question on Stackoverflow
Solution 1 - JavascriptMohsenView Answer on Stackoverflow
Solution 2 - JavascriptchebabyView Answer on Stackoverflow
Solution 3 - JavascriptIain CollinsView Answer on Stackoverflow
Solution 4 - JavascriptBenyamin JafariView Answer on Stackoverflow