【问题标题】:Cannot get Web Service url to work when using Load function in jQuery在 jQuery 中使用 Load 函数时无法使 Web 服务 url 工作
【发布时间】:2012-02-06 17:48:17
【问题描述】:

我就是无法让它工作!每当我单击按钮时,什么都没有发生。已经用 .text('hello world') 替换了 .load 方法,它可以正常工作,所以我的问题似乎出在方法的 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();
}

【问题讨论】:

    标签: c# jquery web-services load asmx


    【解决方案1】:

    虽然这可能不是导致您的问题的原因,但阻止 .Net Web 服务使用 jQuery 的一个常见问题是默认情况下不允许 GETPOST 请求。确保您已使用以下代码在 web.config 中启用适当的方法:

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

    我相信您还可以指定在 WebMethod 级别允许哪些方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多