在使用xmlhttprequest时需要帮助 [英] Need help using xmlhttprequest
问题描述
我正在尝试使用xmlhttprequest将一些xml发送到服务器并获得响应.我知道服务器是正确的,因为它已在另一个类似的应用程序中使用过,但已使用sensha touch完成.我希望有人可以为我指出正确的方向,以说明为什么这段代码对我不起作用. jp只是一点点,它是一个jsp页面,但这只是从上一页获取form变量.哪个有效.
I am trying to use xmlhttprequest to send some xml to a server and get a response. I know the server is right because it has been used in another similar application but was done using sensha touch. I was hoping someone could point me in the right direction as to why this code isn''t working out for me. There is just a bit of jp and it''s a jsp page but that is only to get the form variable from the prior page. which works.
<
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Crib Management</title>
<%
String id = request.getParameter("Eclipse_ID");
String pass = request.getParameter("Password");
%>
<script type="text/javascript">
alert("test");
var id = '<%=id%>';
var pass = '<%=pass%>';
var loginXML = '<?xml version="1.0" encoding="UTF-8"?>';
loginXML += '<IDMS-XML>';
loginXML += '<MobileLogin>';
loginXML += '<Security>';
loginXML += '<Login>';
loginXML += '<LoginID>KRMOBILE</LoginID>';
loginXML += '<Password>KRMOBILE</Password>';
loginXML += '</Login>';
loginXML += '</Security>';
loginXML += '<EclipseID>';
loginXML += id;
loginXML += '</EclipseID>';
loginXML += '<EclipsePassword>';
loginXML += pass;
loginXML += '</EclipsePassword>';
loginXML += '</MobileLogin>';
loginXML += '</IDMS-XML>';
function sendRequest(url,callback,postData) {
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method,url,true);
req.setRequestHeader('User-Agent','XMLHTTP/1.0');
if (postData)
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
// alert('HTTP error ' + req.status);
return;
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
function createXMLHTTPObject() {
var xmlhttp = false;
for (var i=0;i<XMLHttpFactories.length;i++) {
try {
xmlhttp = XMLHttpFactories[i]();
}
catch (e) {
continue;
}
break;
}
return xmlhttp;
}
</script>
</head>
<body>
<%=id%>
<div id="mydiv"><script type="text/javascript">
sendRequest('http:/someurl.com/eserv/eclipse.ecl',handleRequest,'POST');
function handleRequest(req) {
var writeroot = document.getElementById("mydiv");
writeroot.innerHTML = req.responseText;
}
</script>
</div>
</body>
</html>
感谢您的帮助.
当我检查Chrome中的javascript控制台时,我得到了:拒绝设置不安全的标头"User-Agent"
XMLHttpRequest无法加载http://igatedev.kirbyrisk.com:8088/eserv/eclipse.ecl. Access-Control-Allow-Origin不允许使用来源http://igatedev.kirbyrisk.com.
我一直在研究,我只是找到jquery结果.该文件与请求位于同一服务器上,因此我无法确定为什么不允许这样做.
Thanks for any help.
when I check the javascript console in chrome I get this: Refused to set unsafe header "User-Agent"
XMLHttpRequest cannot load http://igatedev.kirbyrisk.com:8088/eserv/eclipse.ecl. Origin http://igatedev.kirbyrisk.com is not allowed by Access-Control-Allow-Origin.
I''ve been looking into this and I just find jquery results. The file is on the same server as the request so I''m not entirly certain why this is unallowed.
推荐答案
您遇到跨域访问限制的问题.当您尝试通过Ajax连接到与HTML源自不同主机的网站时,会发生这种情况.这是为了防止恶意javascript(可能已注入到本来值得信任的文档中)从隐秘地连接到服务器以窃取私人信息的一种措施.
请阅读以下文章,以了解如何规避此功能: ^ ]
问候,
曼弗雷德(Manfred)
You are experiencing issues with cross domain access restrictions. This happens when you are trying to connect to a website via Ajax that is not on the same host where your HTML originated from. This was meant as a measure to fight malicious javascript (which may have been injected into otherwise trustworthy documents) from connecting covertly to a server to steal private information.
Please read the following article to see how you can circumvent this feature: Making cross domain JavaScript requests using XMLHttpRequest or XDomainRequest[^]
Regards,
Manfred
这篇关于在使用xmlhttprequest时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!