簡體   English   中英

“Access-Control-Allow-Origin不允許使用Origin null。”使用jQuery調用Web服務時出錯

[英]“Origin null is not allowed by Access-Control-Allow-Origin.” Error when calling a web service with jQuery

我創建了一個Web服務,它返回一些數據並嘗試使用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錯誤是由於'Origin null is not allowed'錯誤。 這些是我到目前為止采取的步驟:

  • 創建了一個網站(正在進行中)並在網站中創建了Web服務。
  • 我通過在瀏覽器中輸入網址測試了網絡服務,但它確實有效。
  • 創建了一個移動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>

我想也許是因為我沒有用IIS發布網站或網絡服務 - 我需要這樣做嗎? 即使在輸入瀏覽器時url工作,我也不確定它是否應該在客戶端代碼中相同。

我是網絡編程的新手,所以如果你知道什么是錯的,請用簡單的語言解釋,任何幫助都非常感謝。

如果您通過本地Web服務器運行該文件,並且不只是使用瀏覽器打開HTML文件,則問題很可能消失。 當您只是打開文件而不通過Web服務器時,您沒有獲得原始域 - 因此原始為空的錯誤消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM