0

可能重复:
规避同源策略的方法

我正在尝试仅使用 HTML 和 AJAX/JavaScript 从我的计算机调用 Web 服务“ http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius ”。

下面是我的 HTML 和 Ajax 代码。

AJAX 代码:TestWS.js

var xmlhttp = false;
var url;
function createXMLHTTPRequest(){
    if (window.XMLHttpRequest) {

        xmlhttp = new XMLHttpRequest();
        alert("Firefox");

    } 
    else if (window.ActiveXObject) {

        xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
        alert("IE");

    }
    if (!xmlhttp)
        alert("Error initializing XMLHttpRequest!");

}
/* LoadXMLDoc function sets and loads parameters and functions for AJAX calls to the remote server using the POST request method. LOADXMLDoc is called by the send button in the form. */
function loadXMLDoc() {
//  Create XML HTTP Request
    createXMLHTTPRequest();

//  Build the url using field values
    buildURL();
    alert("URL Built");
    var xml = createXMLData();
    alert("XML data created");
//  Build xml-structure
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/FahrenheitToCelsius");
    var xml = createXMLData();
    alert("XML data created");

//  Set function to wait for response 
    xmlhttp.onreadystatechange=xmlhttpChange;


//  Open connection with POST
    xmlhttp.open('POST', url, true);
    alert("Opened");

//  Send request using POST
    xmlhttp.send(xml);
    alert("Sent");
}


function createXMLData() {
    //TODO Gets code from the form. need to create request in code
    var xmlToSend = "";
    xmlToSend += "<?xml version='1.0' encoding='utf-8'?>"; 
    xmlToSend += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "; 
    xmlToSend += "xmlns:xsd='http://www.w3.org/2001/XMLSchema' "; 
    xmlToSend += "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"; 
    xmlToSend += "<soap:Body><FahrenheitToCelsius xmlns='http://tempuri.org/' />"; 
    xmlToSend += "<Fahrenheit>100</Fahrenheit>";
    xmlToSend += "</FahrenheitToCelsius>";
    xmlToSend += "</soap:Body></soap:Envelope>"; 

document.write(xmlToSend);
    return xmlToSend;
}

/* Build the URL to the remote server */
function buildURL() {

    var sAddress = "http://www.w3schools.com/webservices/tempconvert.asmx";

    url = sAddress;

}

/* xmlhttpChange function listens for a response from the remote server. The response can be either text or xml. If xml is received as below the tags are being parsed and presented as needed. */
function xmlhttpChange() {
    if (xmlhttp.readyState==4) {
        if (xmlhttp.status==200) {
            //get the response xml
            var response = xmlhttp.responseXML; 
            alert("Response Recieved");
            document.write(response);
        }
        else if (xmlhttp.status==404) alert("URL doesn't exist!");
        else alert("Problem connecting to server. Status is "+xmlhttp.status);
    }
}

HTML 代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script language="Javascript" src="TestWS.js"></script>
</head>
<body onload="loadXMLDoc()">

</body>
</html>

我收到错误消息:

  1. Chrome 中的状态 0
  2. IE8 权限被拒绝
4

0 回答 0