简体   繁体   English

jquery-ajax-c#成功函数已执行但无响应

[英]jquery-ajax-c# success function is executed but no response

The post is fired and I can see the below in firebug 该职位已被解雇,我可以在萤火虫中看到以下内容

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

jQuery code is: 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");}
});

Then two alerts in the success function are fired but the second alert is empty 然后触发成功功能中的两个警报,但第二个警报为空

Server side coding: 服务器端编码:

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

    return id;
}

currently trying without the involvements of param, the error function is triggeres, no success 当前尝试不涉及参数的情况下,错误函数是触发器,没有成功

jquery code 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"); }

    } );

server code 服务器代码

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

firebug info: 萤火虫信息:

Response Headers 响应标题

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

Request Headers 请求标题

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

In order to see what's wrong. 为了看看有什么问题。

  1. Debug webDelete() to see that id is actually 'abc'! 调试webDelete()以查看ID实际上是'abc'! might be problem in parsing the form data... 解析表单数据时可能会出现问题...

  2. check the actual response using FireBug or Chrome F12 使用FireBugChrome F12检查实际响应


Try navigating to : 尝试浏览至:

http://localhost:1148/WebSite2/frmMain.aspx/webDelete?id=myNeetID
  • does this fire up the WebMethod? 这会启动WebMethod吗?
  • does this return myNeetID or a blank? 这返回myNeetID还是空白?

notice: you might need to Enable GET method. 注意:您可能需要启用GET方法。

If you use [WebMethod][ScriptMethod] in combination, you need to make some changes to your ajax call. 如果结合使用[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");}
});

See: 看到:

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

Thank you all for your help ^_^ 谢谢大家的帮助^ _ ^

Was able to find an answer through this wonderful post! 能够通过这篇精彩的帖子找到答案!

jquery ajax with asp.net not working 使用asp.net的jQuery ajax无法正常工作

Note: This isuue was caused by the .NET version problem 注意:此问题是由.NET版本问题引起的

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

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