2012-08-25 87 views
1

我在http://localhost/Test/Test.asmx?op=HelloWorld託管簡單的ASMX Web服務的Web應用程序在IIS 7.5調用ASMX Web服務方法

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 

namespace TestWebService 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService] 
    public class Test : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public static string HelloWorld() 
     { 
      return "Hello World"; 
     } 

    } 
} 

當我點擊調用按鈕,它工作正常,但當我嘗試調用此方法使用在ASP.NET開發服務器(http://localhost:1756/HTMLPage1.htm)上運行的不同Web應用程序項目中的$ .ajax()我得到一個錯誤response.status爲0,無法調用Web方法,這裏是我的代碼。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Consume Test </title> 
    <script src="jquery-1.5.1.js" type="text/javascript"></script> 
    <script type="text/javascript"> 

     function displayMessageCall() { 

      $.ajax({ 
       type: "POST", 
       url: "http://localhost/Test/Test.asmx/HelloWorld", 
       data: '{}', 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: successMsg, 
       error: errorMsg 

      }); 
     } 

     function successMsg(response) { 
      alert('Success'); 
     } 

     function errorMsg(response) { 
      alert(response.status + " " + response.statusText) 
     } 

     $(document).ready(function() { 
      displayMessageCall(); 
     }); 
    </script> 
</head> 
<body> 
</body> 
</html> 

我錯過了什麼嗎?

+0

我已經找到了使用easyXDM來實現這個目的的方法http://easyxdm.net/wp/2010/03/17/setting-up-your-first-socket/ – ddfnfal

回答

0

我想你只是碰到了same origin policy

由於端口的差異,瀏覽器限制了javascript到達web服務。當然,有一些workarounds

+0

嗨,感謝您指出我的內容和HTML5解決方法,我已經找到了另一個解決我的問題無論如何感謝幫助我... – ddfnfal