I'm trying to get the USPS address verification API working using Javascript's XMLHTTP protocol. I'm successfully sending data, but the USPS engine doesn't like something. I get the following response through xmlhttp.responseText
:
<Error><Number>80040B19</Number><Description>XML Syntax Error: Please check the XML request to see if it can be parsed.</Description><Source>USPSCOM::DoAuth</Source></Error>
Below is the code I'm using to test the construct. You can see an alert()
that copies out the XML, which I've run through online parse checkers with success. I've reviewed a lot of SO questions relating to cURL, etc., but I've not found one relating to XMLHTTP. But, to be honest, I'm not sure XMLHTTP is the problem. It's sending and receiving. Is it a problem with how I'm building the XML request? I'm closely modeling the API example. Thanks!
<html><script language="javascript">
var xmlhttp;
var formData = new FormData();
formData.append('API', 'Verify');
$xml = '<AddressValidateRequest USERID="XXXX">\
<Address>\
<Address1></Address1>\
<Address2>450 Bauchet St.</Address2>\
<City>Los Angeles</City>\
<State>CA</State>\
<Zip5></Zip5>\
<Zip4></Zip4>\
</Address>\
</AddressValidateRequest>';
formData.append('XML', $xml);
alert( $xml+"\n\n");
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
console.log(xmlhttp.readyState);
console.log(xmlhttp.status);
if(xmlhttp.readyState==4 && xmlhttp.status==200){
alert(xmlhttp.responseText);
if(xmlhttp.responseText == 'false'){ alert(xmlhttp.responseText); }
}
}
xmlhttp.onerror = function () { console.error(xmlhttp.statusText); }
xmlhttp.open("POST", 'http://production.shippingapis.com/ShippingAPI.dll');
xmlhttp.send(formData);
</script> </html>
The interface and the sign-up for the service come from here. USPS.com.
Try using "GET" as the METHOD? THE USPS API is very easy to use. When POSTing make sure to have the body set up properly