2012-02-06 19 views
1

我只是无法让它工作!每当我点击按钮什么都没有发生。用.text('hello world')代替了.load方法,它的工作原理好像我的问题出现在方法的url中。已经到处寻找答案,但我认为我必须忽略一些非常明显的事情,因为在任何地方都没有看到过同样的东西。请帮助它让我疯狂!在jQuery中使用Load函数时无法使Web Service url正常工作

客户端:

<script src="Scripts/jQuery-1.7.1.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
    // Cannot get this to work 
     $(function() { 
      $('#buttonSays').click(function() { 
       $('div').load('AjaxServices.asmx/HelloWorld'); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <h1>Hello World from Web Service</h1> 
    <button type="button" id="buttonSays">Get Info</button> 
    <p>The site says...</p> 
    <div id="divSays1"></div> 
    <div id="divSays2"></div> 
    <div id="divSays3"></div> 
</body> 

服务器端:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptService] 
public class AjaxServices : System.Web.Services.WebService 
{ 

private static int count = 0; 

[WebMethod] 
public string HelloWorld() 
{ 
    count++; 
    return "Hello World #" + count.ToString(); 
} 

回答

2

虽然这未必是什么导致你的问题,即防止的.NET Web服务从jQuery是工作的一个常见问题GETPOST请求默认情况下不允许。请确保您已启用使用下面的代码在你的web.config适当的方法(S):

<webServices> 
    <protocols> 
     <add name="HttpGet"/> 
     <add name="HttpPost"/> 
    </protocols> 
</webServices> 

,我相信你也可以指定哪些方法(S)在水平的WebMethod允许

[ScriptMethod(UseHttpGet = true)] 
public string HelloWorld() 
{ 
    count++; 
    return "Hello World #" + count.ToString(); 
} 
+0

哇!非常感谢你!! – user1192900 2012-02-06 18:05:17

相关问题