3

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.

Hari Chaudhary
  • 630
  • 1
  • 7
  • 20
  • I do suggest you insert then exact text of your **error messages** here for the benefit of future googlers. And/or use [FastStone Screen Capture](http://www.faststone.org/FSCaptureDetail.htm) for a visual. (Sheesh I'm advertising today like a racecar driver. Though less paid.) – Bob Stein Feb 11 '15 at 14:43

1 Answers1

2

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.

Community
  • 1
  • 1
Bob Stein
  • 16,271
  • 10
  • 88
  • 101
  • Thanks @BobStein, It worked, I made request to USPS server through server-side (C# ASP.NET) code and got the response. – Hari Chaudhary Feb 11 '15 at 14:26
  • Great @Hari! I was about to ask you for code or specifics when I got it, you were trying to do what I was planning to do, and really should someday be the better solution (direct instead of relaying). By the way I'm implementing a [free-form](http://stackoverflow.com/q/11160192/673991) smartystreets [jQuery plugin](https://smartystreets.com/docs/plugin) instead, very class act. Shame no one validates non-US addresses that well however. – Bob Stein Feb 11 '15 at 14:37
  • 1
    Yes, right. I am struggling for Canada address validations. – Hari Chaudhary Feb 11 '15 at 15:05