Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how Postman send requests? ajax, same origin policy

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' );         }     }     }); }, 
like image 776
Joey Hipolito Avatar asked Apr 15 '13 17:04

Joey Hipolito


2 Answers

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

like image 58
Mohsen Avatar answered Oct 05 '22 13:10

Mohsen


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

like image 38
chebaby Avatar answered Oct 05 '22 15:10

chebaby