how Postman send requests? ajax, same origin policy

64,519

Solution 1

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

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

Content-Type      application/json

X-Requested-With  XMLHttpRequest

Screenshot

Send Ajax request in postman

Solution 3

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.

Share:
64,519
Joey Hipolito
Author by

Joey Hipolito

I'm a rather curious being that asks a lot of questions. I've learned the hard way before and now trying to ease things up by asking and trying.

Updated on October 13, 2021

Comments

  • Joey Hipolito
    Joey Hipolito over 2 years

    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' );
            }
        }
        });
    },
    
  • CodyBugstein
    CodyBugstein over 9 years
    How can that be? Doesn't the website itself have to also allow CORS?
  • Mohsen
    Mohsen over 9 years
    @Imray There is no need for CORS headers, just like a server program or curl.
  • CodyBugstein
    CodyBugstein over 9 years
    So basically, a Chrome app is not subject to Cross Origin security? So a Chrome app can access my bank cookies, or my Facebook login?
  • Mohsen
    Mohsen over 9 years
    @Imray Chrome apps can bypass Cross Origin. Chrome will not share your regular browsing sessions with this permission. But if you install a Chrome extension that can execute content script sure they can access your bank cookies!
  • CodyBugstein
    CodyBugstein over 9 years
    dang that's a big uneasing
  • Pacerier
    Pacerier over 9 years
    @Imray, Obviously if you click "Allow This" and "Allow That" the app can do anything it wants among the privileges that you've allowed it to.
  • nr5
    nr5 over 4 years
    I am trying to hit an ajax request via postman. The ajax request is sending dataType:` json` and data: {loginId: "[email protected]", client: "698983"}. While going into the postman, I am trying to send body parameters as JSON with all the above info and Content-Type: application/json in headers but it fails with 500. Any help ?