开发者

XML Response Parsing

开发者 https://www.devze.com 2023-01-08 03:57 出处:网络
Sorry if my question is a little repetitive, but I did not find an answer for my question so I post it here.

Sorry if my question is a little repetitive, but I did not find an answer for my question so I post it here.

So, here is this URL which I use to generate a security-token:

api.sandbox.inrix.com/Traffic/Inrix.ashx?Action=GetSecurityToken&vendorId=1043016094&consumerId=94ce0781-b32f-4da5-b80b-8ca00cfb2194

The response of typing the above URL in the browser is an XML Tree being displayed on the browser.I need to extract the data within the tags "AuthToken". I wrote a code in JavaScript that extracts data from tag开发者_如何学JAVAs from an XML file and instead of specifying the path of the XML File, I passed it the above URL. However, I get anAlert Message saying:

Access to Restricted URI Denied.

Any suggestions?

Thanks.


A security feature of all browsers prevents you from making XMLHttpRequests to other domains. This is called the Same Origin Policy. There are a few ways to get around the same origin policy,

1. Provide a proxy to the service using a server-side language.
Normally, you would make requests directly to a web service, which would in turn return a response, like so:

  ╒═══════════════════╕   ──────────────>   ╒═══════════════════╕  
  │ Client/Javascript │                     │ Server/WebService │
  ╘═══════════════════╛   <──────────────   ╘═══════════════════╛  

Proxying involves writing a server-side script to act as a middle-man:

 ╒═══════════════════╕ ────> ╒════════════════════╕ ────> ╒═══════════════════╕  
 │ Client/Javascript │       │ Same domain server │       │ Server/WebService |
 ╘═══════════════════╛ <──── ╘════════════════════╛ <──── ╘═══════════════════╛  

So the browser is making a request to the same domain, it isn't blocked by the same-origin policy. The server in turn makes the request to the remote web service, which returns the response. Finally, the same domain server returns that response to the script, which is still waiting. This works because requests made by the server aren't subject to the same-origin policy.

2. Check to see if the service offers a JSONP data format.
JSONP works by adding a script element to the current page, with the src attribute pointing to the web service. The web service returns the response in the form of a JavaScript function call with the data in a JavaScript object literal that is passed as the argument to the function. All you need to do is predefine that function so that when the script/webservice request completes your predefined function is called with the data so that you can handle it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号