2013-07-16 55 views
1

的职位被解雇,我可以看到下面的萤火jQuery的AJAX-C#成功的功能,但没有任何反应

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; 
} 

目前正在努力没有参数的时受累,误差函数是triggeres,没有成功

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 

回答

1

为了能看到什么是错的。

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

  2. 检查使用萤火铬F12的实际响应


尝试导航到:

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

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

+0

1.代码是没有得到执行2.实际响应是在客户端页面3.怎样的HTML代码引起的在此评论中添加新行!? – rps

+0

什么也没有发生,我没有在firebug中看到任何ajax文章,所以没有请求,也没有响应,如果我点击那个url,页面就会加载。 – rps

+0

启用GET方法? – rps

1

如果组合使用[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/

+0

我会让你试试看,但为什么JSON?因为'ScriptMethod'告诉服务器方法该请求是通过脚本触发的?起初,我没有包括那个('ScriptMethod'),它在那个时候也没有工作。 – rps

+0

控制转到错误功能;更改** $。toJSON **到** JSON.stringify ** coz得到错误“$ .toJSON不是函数”,我也从服务器端删除了内容类型。 “失败”的警报被解雇了 – rps

+0

好的。抱歉,toJSON()不是标准JQuery的一部分。从服务器端删除数据类型也是可以的。你能调试与Firebug的沟通吗?这将使我们更好地了解请求发生了什么。 –