我有一些非常簡單的JavaScript代碼與兩個按鈕。第一個提交調用Web服務函數的表單,並在新窗口中打開該表單。JavaScript WebService NS_ERROR_DOM_BAD_URI
第二個按鈕應該做同樣的事情,除了我想把Web服務的響應文本放到頁面上的div上。
的web服務,從一個例子,我發現here來(你可以在底部下載該項目中的zip文件。)
- 有在WebService.asmx標記一個錯字,其中類屬性應該是「Encosia.Samples.ASMX_CORS.WebService」。
該Web服務已允許所有來源<add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" />
所以不應該有跨站點腳本的問題。
JavaScript代碼(注:是由Visual Studio生成的本地主機名當我運行Web服務項目)
<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>
當我點擊第一個按鈕,一個新頁面打開顯示XMLHttpRequest
的XML結果。當我點擊第二個按鈕是HttpFox告訴我,我有一個:
NS_ERROR_DOM_BAD_URI錯誤
的結果只是一個空字符串。
我也試過jQuery
正是如此具有相同的結果:
function WebRequestTest2(){
$.ajax({
type: "GET",
url: "http://localhost:53276/WebService.asmx/HelloWorld",
dataType: "xml",
success: function(xml) {
var myString = $(xml).find('string').text();
}
});
}
爲了進一步雪上加霜的是,JavaScript代碼在上面IE 8能正常工作,而在Firefox 17這是我的目標瀏覽器。
任何人都可以告訴我如何使Firefox與我的Web服務很好嗎?有人向我建議,我可能需要在我的Web服務中使用CORS庫,但看起來這是一個常見的問題,必須有一個已經定義好的解決方案。
這是否假設爲POST或GET?什麼是響應的MIME類型? –
理想情況下發布,但我還沒有能夠得到任何一個到目前爲止的工作。數據類型應該是application/x-www-form-urlencoded,我相信這是默認的,對嗎? 編輯:響應應該返回一個字符串,如' Hello World ' –
nickvans