JavaScript Web服务NS\u错误\u DOM\u错误\u URI
我有一些非常简单的javascript代码,带有两个按钮。第一个提交一个调用web服务函数的表单,并在新窗口中打开它JavaScript Web服务NS\u错误\u DOM\u错误\u URI,javascript,jquery,web-services,cors,Javascript,Jquery,Web Services,Cors,我有一些非常简单的javascript代码,带有两个按钮。第一个提交一个调用web服务函数的表单,并在新窗口中打开它 <head> <title>Test Web Service Using JavaScript</title> </head> <body> <form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post"
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>
第二个按钮应该做同样的事情,只是我想把来自web服务的响应文本放到页面上的一个div中
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>
web服务来自我找到的一个示例(您可以在底部的zip文件中下载该项目)
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>
- WebService.asmx标记中有一个输入错误,其中Class属性应该是“Encosia.Samples.asmx_CORS.WebService”李>
因此跨站点脚本编写应该不会有问题
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>
JavaScript代码(注意:本地主机名是在我运行web服务项目时由Visual Studio生成的)
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>
雪上加霜的是,上面的JavaScript代码在IE8中运行良好,但在我的目标浏览器Firefox17中却不行
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>
有人能告诉我如何让Firefox在我的web服务中发挥作用吗?有人建议我在我的web服务中可能需要一个CORS库,但这似乎是一个问题,这个常见的库必须内置一个已定义的解决方案。我通过使用WebAPI CORS功能解决了我的问题,如下示例:
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>
此外,我还需要覆盖firefox发送的源文件,因为在本地加载文件时它是空的。我使用HttpFox来做这件事。这应该是一个
POST
还是GET
?什么是mime类型的响应?理想情况下是POST,但到目前为止,我还没能让其中任何一个起作用。数据类型应该是application/x-www-form-urlencoded,我认为这是默认值,对吗?编辑:响应应返回一个字符串,如Hello World
<head>
<title>Test Web Service Using JavaScript</title>
</head>
<body>
<form action='http://localhost:53276/WebService.asmx/HelloWorld' method="post" target="_blank">
<input type="submit" value="This Works" class="button">
<label> This opens a new window with the result of the web service call</label>
</form>
<input type='submit' id='btnTest' name='btnTest' value="This Doesn't" onclick="WebRequestTest();" >
<label> This should fill the div with the result of the web service call</label>
<div id="MyDiv"></div>
</body></html>
<script language="javascript">
function WebRequestTest() {
function handler() {
//fires when ready state changes
if (this.readyState == 4 ){ //&& request.responseText != '') {
var response = request.responseText;
document.getElementById('MyDiv').innerHTML = response;
return;
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = handler;
request.withCredentials = true;
request.open('POST', 'http://localhost:53276/WebService.asmx/HelloWorld', true); //third optional argument: async (default true)
request.send();
}
</script>