简体   繁体   English

jQuery Ajax()无法达到C#Webmethod

[英]jQuery Ajax() can't reach C# Webmethod

I don't get why $.ajax() function cannot reach my [WebMethod]. 我不明白为什么$ .ajax()函数无法到达我的[WebMethod]。

Here is the jQuery below: 这是下面的jQuery:

$('.BasketUpdaterSubmit').click(function() {
$.ajax({
    url: '/Ajax/AjaxCalls.aspx/UpdateAsyncBasket',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{'name' : 'Ivan'}",
    success: function(data) { alert(data); },
    error: function(xhr) { alert("Damn!"); }
});

Here is the C# code: 这是C#代码:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string UpdateAsyncBasket(string name)
    {
        // random stuff
        return "Received : \t " + name;
    }

When I place a breakpoint on the return statement I never seem to get there. 当我在return语句上放置断点时,我似乎从未到达那里。 What am I doing wrong? 我究竟做错了什么?

根据我对这些东西的经验,我认为必须将JS放在.NET的页面加载javascript函数内部,以访问C#Web方法。

function pageLoad(sender, eventArgs) { }

Try this code, 试试这个代码,

$(document).on("click",".BasketUpdaterSubmit",function(){    
    $.ajax({
        url: '/Ajax/AjaxCalls.aspx/UpdateAsyncBasket',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'name' : 'Ivan'}",
        success: function(data) { alert(data); },
        error: function(xhr) { alert("Damn!"); }
    });    
});

And the web.config you have to add following section inside the system.web section 而web.config您必须在system.web部分中添加以下部分

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

Above code should work(tested in my machine). 上面的代码应该可以工作(在我的机器上测试)。 My best suggestion for you is create a web service for this. 我对您的最佳建议是为此创建一个Web服务。 so you can omit the page life cycle. 因此您可以省略页面生命周期。 You can refer following sample to learn how to do it properly http://tutorials.cmsnsoftware.com/2011/01/how-to-call-csharp-function-in-ajax.html 您可以参考以下示例以了解如何正确执行此操作http://tutorials.cmsnsoftware.com/2011/01/how-to-call-csharp-function-in-ajax.html

Try a GET instead of a POST . 尝试使用GET而不是POST I have a few web methods that work fine using similar javascript, but have decorated the method with this instead of just [WebMethod] : 我有一些使用相似的javascript可以正常工作的网络方法,但是用此装饰了方法,而不仅仅是[WebMethod]

[WebMethod, ScriptMethod(UseHttpGet = true)]

And then make sure your ajax call specifies GET: 然后确保您的ajax调用指定GET:

$('.BasketUpdaterSubmit').click(function() {
$.ajax({
    url: '/Ajax/AjaxCalls.aspx/UpdateAsyncBasket',
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{'name' : 'Ivan'}",
    success: function(data) { alert(data); },
    error: function(xhr) { alert("Damn!"); }
});

We figured out what's going on. 我们知道发生了什么事。 It seems that there is a default ajax setup function which was interfering with my jquery function and didn't proceed when testing. 似乎有一个默认的ajax设置函数正在干扰我的jquery函数,并且在测试时没有进行。 Most probable reason is improper parameter handling. 最可能的原因是参数处理不正确。 Sorry for the late response. 回复晚了非常抱歉。 Thank you all. 谢谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM