使用jQuery发送XML请求

时间:2022-03-20 15:32:28

I want to communicate with the UPS API via jQuery and AJAX. The XML seems to be well formed, as it works fine in a similar ASP script. But I figured I would skip the ASP script and communicate directly with UPS via jQuery. However, when I send the request, the response comes up as:

我想通过jQuery和AJAX与UPS API进行通信。 XML似乎很好地形成,因为它在类似的ASP脚本中工作正常。但我想我会跳过ASP脚本并通过jQuery直接与UPS通信。但是,当我发送请求时,响应会显示为:

METHOD: OPTIONS
Status: Load Cancelled

方法:选项状态:加载已取消

I assume I missed some kind of option in the ajax request that tells it to send the xml to the UPS server.

我假设我在ajax请求中错过了某种选项,告诉它将xml发送到UPS服务器。

    $(document).ready(function() {
            $.ajax({ type: "POST",
                            url: "https://wwwcie.ups.com/ups.app/xml/XAV",
                            data: "<?xml version=1.0?>" + 
                                "   <AccessRequest xml:lang='en-US'>" +
                                "       <AccessLicenseNumber>LICENSENUMBER</AccessLicenseNumber>" +
                                "       <UserId>USER</UserId>" +
                                "       <Password>PASS</Password>" +
                                "   </AccessRequest>" +
                                "   <?xml version=1.0?>" +
                                "   <AddressValidationRequest xml:lang='en-US'>" +
                                "       <Request>" +
                                "           <TransactionReference>" +
                                "               <CustomerContext>AddressValidationRequest</CustomerContext>" +
                                "               <XpciVersion>1.0</XpciVersion>" +
                                "           </TransactionReference>" +
                                "           <RequestAction>XAV</RequestAction>" +
                                "           <RequestOption>3</RequestOption>" +
                                "       </Request>" +
                                "       <AddressKeyFormat>" +
                                "           <AddressLine>1234 N. Main Street</AddressLine>" +
                                "           <PoliticalDivision2>Los Angeles</PoliticalDivision2>" +
                                "           <PoliticalDivision1>CA</PoliticalDivision1>" +
                                "           <PostcodePrimaryLow>90210</PostcodePrimaryLow>" +
                                "           <CountryCode>US</CountryCode>" +
                                "       </AddressKeyFormat>" +
                                "   </AddressValidationRequest>", 
                            contentType: "text/xml",
                            dataType: "xml",
                            cache: false,
                            error: function() { alert("No data found."); },
                            success: function(xml) {
                                alert("it works");
                                alert($(xml).find("project")[0].attr("id"));
                            }
            });
        });

1 个解决方案

#1


0  

It seems you are making a Cross Domain call.
You would need to use jsonp for Cross Domain calls. Refer ajax

您似乎正在进行跨域呼叫。您需要使用jsonp进行跨域调用。请参考ajax

crossDomain (default: false for same-domain requests, true for cross-domain requests) Type: Boolean If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)

crossDomain(默认值:对于同域请求为false,对于跨域请求为true)类型:Boolean如果要在同一域上强制执行跨域请求(如JSONP),请将crossDomain的值设置为true。例如,这允许服务器端重定向到另一个域。 (版本增加:1.5)

#1


0  

It seems you are making a Cross Domain call.
You would need to use jsonp for Cross Domain calls. Refer ajax

您似乎正在进行跨域呼叫。您需要使用jsonp进行跨域调用。请参考ajax

crossDomain (default: false for same-domain requests, true for cross-domain requests) Type: Boolean If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)

crossDomain(默认值:对于同域请求为false,对于跨域请求为true)类型:Boolean如果要在同一域上强制执行跨域请求(如JSONP),请将crossDomain的值设置为true。例如,这允许服务器端重定向到另一个域。 (版本增加:1.5)