Search code examples
ajaxcorsusps

Cross Domain access to USPS Address validation API


I am trying to hit USPS address validation API by AJAX call and I am getting 'Cross-Origin Request Blocked' error.

However when I am trying to access the same address though URL, I am getting the results.

Has this error something to do with approval thing?

The id I am using for this has been approved and I am able to get the result by the API through URL.

I want to access it though scripting. Please help.

Any help would be appreciated.

Thanks in advance.


Solution

  • I think the way you might want to do this is to have the Ajax requests go to your web server -- the same HTTP server where the page came from. And then your server-side program (in PHP, or Python or whatever you're using) relays the request to the USPS server.

    Making the request browser-side, from Ajax directly to the USPS server, is just the kind of suspicious activity (called Cross-Site Scripting) that browsers are learning to block.

    CORS could theoretically be one way to unblock a Cross-Origin Request, but in that scheme it would be the USPS API server that ultimately grants that permission for pages from your domain. Tech detail: by inserting an Access-Control-Allow-Origin header in response to a request with an Origin header in the Ajax back-and-forth.