ajax 使用 XMLHttpRequest.Open 跨浏览器访问时权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/355185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permission Denied When using XMLHttpRequest.Open cross browser access
提问by user42070
I am trying to access XMLHTTPRequest.open Method I have even included netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
我正在尝试访问 XMLHTTPRequest.open 方法我什至包括 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
but still its not working.
但仍然无法正常工作。
I am using javascript and HTML to access the WebService.
我正在使用 javascript 和 HTML 来访问 WebService。
Any Help would be really great
任何帮助都会很棒
Code
代码
<html>
<Head>
<Title>Calling A WebService from HTML </Title>
</Head>
<Body onload='GetDataFrmWS()'>
<form name="Form1" id="Form1" runat="server" method="post">
<div id="DisplayData" > </div>
<div id="Menu2"></div>
</form>
<script language='javascript'>
var objHttp;
var objXmlDoc;
function GetDataFrmWS()
{
alert('I M Here');
var func = getDataFromWS();
}
function getDataFromWS()
{
if(window.ActiveXObject)
{
try
{
objHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (ex)
{
objHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
}
else if (window.XMLHttpRequest)
{
objHttp = new window.XMLHttpRequest();
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
strEnvelope = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
' <HelloWorld xmlns="http://tempuri.org/">' +
' <Dummy xsi:type="xsd:string">Hello</Dummy>'+
' </HelloWorld>'+
'</soap:Body>' +
'</soap:Envelope>' ;
var szUrl;
szUrl = 'http://kamadhenu/Quoteme/GetCategories.asmx?op=HelloWorld';
objHttp.onreadystatechange = HandleResponse;
objHttp.open('POST', szUrl, true);
objHttp.setRequestHeader('Content-Type', 'text/xml');
objHttp.setRequestHeader('SOAPAction','http://tempuri.org/HelloWorld');
objHttp.send(strEnvelope);
}
function HandleResponse()
{
if (objHttp.readyState == 4)
{
if (window.ActiveXObject)
{
objXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
objXmlDoc.async="false";
objXmlDoc.loadXML(objHttp.responseText);
var nodeSelect = objXmlDoc.getElementsByTagName("Menu1").item(0);
var Menu2=objXmlDoc.getElementsByTagName("Menu2").item(0);
document.getElementById('DisplayData').innerHTML=nodeSelect.text;
document.getElementById('Menu2').innerHTML=Menu2.text;
}
else
{
var Text=objHttp.responseText;
var parser=new DOMParser();
objXmlDoc = parser.parseFromString(Text,'text/xml');
var Value=objXmlDoc.documentElement.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue;
var Menu2=objXmlDoc.documentElement.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0].nodeValue;
var Menu3=objXmlDoc.documentElement.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[1].nodeValue;
document.getElementById('DisplayData').innerHTML=Value;
document.getElementById('Menu2').innerHTML=Menu2;
document.getElementById('Menu2').innerHTML+=Menu3;
}
}
}
</script>
<input type='Button' Text='Click Me' onclick='GetDataFrmWS()' value="Click Me!"/>
°
</Body>
</HTML>
回答by M.N
Browser Independent code for XML HTTPRequest
XML HTTPRequest 的浏览器独立代码
I use the following code to create an XML object. It has been designed to handle all browsers (esp. IE and non IE)
我使用以下代码创建一个 XML 对象。它旨在处理所有浏览器(尤其是 IE 和非 IE)
/* Function to create an XMLHTTP object for all browsers */
function getXMLHTTPObject(){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
return xmlHttp;
}
/* End Function */
P.S. You code in the question is not readable. Pls format it
PS 您在问题中的代码不可读。请格式化它
回答by Lucacri
As far as I know, the XMLHTTP request must point to a page on the same subdomain of the html page for the various browsers permissions.
据我所知,对于各种浏览器权限,XMLHTTP 请求必须指向 html 页面的同一子域上的页面。
One trick is to make another page on the same server in your preferred language and make it handle the request with the server's network.
一个技巧是在同一台服务器上用您的首选语言制作另一个页面,并使其处理与服务器网络的请求。
Example:
例子:
from your HTML page you make a ajax request to mydomain.com/externalrequest.php?url=www.google.com and that page will connect (fsock/cURL etc) to "url" and return it
从您的 HTML 页面向 mydomain.com/externalrequest.php?url=www.google.com 发出 ajax 请求,该页面将连接(fsock/cURL 等)到“url”并返回它
回答by Genericrich
If you are TRYING to go cross-domain with XHR, you can look into the JSONP method. Check JQuery docs for that.
如果您正在尝试使用 XHR 进行跨域,您可以查看 JSONP 方法。检查 JQuery 文档。
Would require you to accept JSON response but it does work across domains.
需要您接受 JSON 响应,但它确实可以跨域工作。
回答by Strelok
There is a pretty concise example here
有一个相当简洁的例子在这里
Try making your URL http://recpushdata.cyndigo.com/Jobs.asmx/InsertXML
尝试使您的 URL http://recpushdata.cyndigo.com/Jobs.asmx/InsertXML
PS. Your code is unreadable in StackOverflow.
附注。您的代码在 StackOverflow 中不可读。

