【问题标题】:jquery-ajax-c# success function is executed but no responsejquery-ajax-c#成功函数执行但没有响应
【发布时间】:2013-07-16 11:42:54
【问题描述】:

帖子已被解雇,我可以在 firebug 中看到以下内容

POST http://localhost:1148/WebSite2/frmMain.aspx/webDelete 200 OK 15ms

jQuery代码是:

$.ajax({
   url: "frmMain.aspx/webDelete",
   type: "POST",  
   dataType: "text",
   contentType:"text/plain",
   data: {id:"abc"},
   success: function(data){alert("success");alert(data)},
   error: function(){alert("failed");}
});

然后成功函数中的两个警报被触发,但第二个警报为空

服务器端编码:

[WebMethod][ScriptMethod]
public static string webDelete(string id)
{
    HttpContext context = HttpContext.Current;
    context.Response.ContentType = "text/plain";

    return id;
}

目前正在尝试不涉及param,错误函数是triggers,没有成功

jquery 代码

$.ajax({

        url: "frmMain.aspx/webDelete",
        type: "POST",  
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        async: true,            
        success: function(data){alert("success");alert(data.d) },
        error: function(){alert("failed"); }

    } );

服务器代码

[WebMethod][ScriptMethod]
public static string webDelete()
{
   return "hello";
}

萤火虫信息:

响应标头

Cache-Control   private
Connection          Close
Content-Length  11732
Content-Type    text/html; charset=utf-8
Date            Thu, 18 Jul 2013 09:47:34 GMT
Server          ASP.NET Development Server/8.0.0.0
X-AspNet-Version    2.0.50727

请求标头

Accept          application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length  2
Content-Type    application/json; charset=utf-8
Host            localhost:1148
Referer         http://localhost:1148/WebSite2/frmMain.aspx
User-Agent          Mozilla/5.0 (Windows NT 5.2; rv:22.0) Gecko/20100101 Firefox/22.0
X-Requested-With    XMLHttpRequest

【问题讨论】:

    标签: c# jquery


    【解决方案1】:

    为了看看有什么问题。

    1. 调试 webDelete() 以查看 id 实际上是 'abc'! 解析表单数据可能有问题...

    2. 使用 FireBugChrome F12

    3. 检查实际响应

    尝试导航到:

    http://localhost:1148/WebSite2/frmMain.aspx/webDelete?id=myNeetID
    
    • 这会启动 WebMethod 吗?
    • 这会返回 myNeetID 还是空白?

    注意:您可能需要启用 GET 方法。

    【讨论】:

    • 1.该代码没有被执行 2. 实际响应是客户端页面的 html 代码 3. 如何在此评论中添加新行!?
    • 什么都没有发生,我在 firebug 中看不到任何 ajax 帖子,所以没有请求,没有响应,如果我点击该 url,页面就会加载。
    • @rps 仅用于测试建议。一些配置不允许 GET 请求。 support.microsoft.com/kb/819267
    【解决方案2】:

    如果你结合使用[WebMethod][ScriptMethod],你需要对你的ajax调用做一些改变。

    $.ajax({
       url: "frmMain.aspx/webDelete",
       type: "POST",  
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       data: JSON.stringify({id:"abc"}),
       success: function(data){alert("success");alert(data.d)},
       error: function(){alert("failed");}
    });
    

    见:

    http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

    【讨论】:

    • 我会在尝试后告诉你,但为什么是 JSON?因为'ScriptMethod'告诉服务器方法请求是通过脚本触发的?起初我没有包括那个('ScriptMethod'),当时它也没有工作。
    • 控制进入错误功能;将 $.toJSON 更改为 JSON.stringify 因为收到错误“$.toJSON 不是函数”,我还从服务器端删除了内容类型。 “失败”的警报被触发
    • 嗯,好的。很抱歉 toJSON() 不是标准 JQuery 的一部分。从服务器端删除数据类型也可以。你能调试与 Firebug 的通信吗?这将使我们更好地了解请求发生了什么。
    • http://localhost:1148/WebSite2/frmMain.aspx/webDelete 那是萤火虫中的请求链接,我想它缺少参数? data: {id:"abc"} 这就是我在 JQuery 代码中给出参数的方式,在萤火虫的 post 视图中我看到 Source --- id=abc
    • 数据应该是 POST 消息的一部分。由于您正在执行 POST,因此您看不到任何参数作为 URL 的一部分。源应作为 JSON 发送。不像通常那样作为键/值集合
    【解决方案3】:

    谢谢大家的帮助^_^

    能够通过这篇精彩的帖子找到答案!

    jquery ajax with asp.net not working

    注意:这个问题是.NET版本问题引起的

    【讨论】:

      猜你喜欢
      • 2015-06-13
      • 2016-03-25
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      相关资源
      最近更新 更多