0
網絡服務時
我創建了返回一些數據的Web服務,我試圖用jQuery來訪問它,但控制檯顯示以下錯誤:「Access-Control-Allow-Origin不允許原始位置」。錯誤調用使用jQuery
OPTIONS http://localhost:56018/PhoneWebServices.asmx?op=GetMyChildren 405 (Method Not Allowed)
jQuery.ajaxTransport.sendjquery-1.7.1.js:8102
jQuery.extend.ajaxjquery-1.7.1.js:7580
LoginButton_onclickindex.html:26
(anonymous function)index.html:59
onclickindex.html:60
XMLHttpRequest cannot load http://localhost:56018/PhoneWebServices.asmx?op=GetMyChildren. Origin null is not allowed by Access-Control-Allow-Origin.
我承擔的405錯誤是由於'原因不允許'錯誤。 這些是我到目前爲止所採取的步驟:
- 創建了一個網站(正在進行中)並在網站中創建了網絡服務。
- 我通過在瀏覽器中輸入網址來測試網絡服務,並且它可以正常工作。
- 創建了一個移動web應用程序,它試圖調用web服務,但顯示上述錯誤。
我的客戶端(移動應用程序的代碼):
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
}
function LoginButton_onclick() {
var email=document.getElementById("EmailBox").value;
var pass=document.getElementById("PasswordBox").value;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:56018/PhoneWebServices.asmx?op=GetMyChildren",
data: '{ "email" : "' + email + '", "password": "' + pass + '" }',
dataType: "json",
success: GetChildrenSuccess,
failure: GetChildrenFailed
});
}
function GetChildrenSuccess(response) {
var children = eval('(' + response.d + ')');
var child;
for(child in children) {
$('#ResultsDiv').innerHTML = "ID: "+child.ID+ " Name: "+child.Name+" Surname: "+child.Surname+" \r\n";
}
}
function GetChildrenFailed(error) {
document.getElementById('ResultsDiv').innerHTML = "Error";
}
</script>
我想也許是因爲我沒有發佈網站或web服務與IIS的,我需要做到這一點?即使網址在瀏覽器中輸入時仍然有效,但我不確定它在客戶端代碼中是否應該是相同的。
我對網絡編程非常陌生,所以如果你知道什麼是錯的,請用簡單的語言來解釋它,任何幫助都將不勝感激。
所以爲了解決這個問題,我需要在IIS中託管我的網站嗎? – Matt 2012-03-31 16:26:04
@Matt是的,IIS或您選擇的其他網絡服務器。 – 2012-03-31 16:37:29
好的,謝謝你的幫助:) – Matt 2012-03-31 16:39:02