2011-02-09 50 views
0

我使用一个jQuery函数这有点像这样调用页面方法:页方法不工作

function GetNewDate(thedateitem) { 

     ThisMonth = 3; 
     TheDay = 1; 
     TheYear = 2011; 

    DateString = TheMonth + '/' + TheDay + '/' + TheYear; 

    $.ajax({ 
     type: "POST", 
     url: "/Pages/CallHistory.aspx/ResetDate", 
     contentType: "application/json; charset=utf-8", 
     data: DateString, 
     dataType: "json", 
     success: successFn, 
     error: errorFn 
    }) 
}; 

,然后在后面的文件中的代码,我有:

[WebMethod] 
public static void ResetDate(DateTime TheNewDate) 
{ 
    var test = 4; 
} 

然而,当我在var test = 4上放置一个断点时,它永远不会停在那里。

我错过了什么?

谢谢。

+0

我总是用数据类型:“文本“当调用Web方法时,只是指出这一点,并不确定这是否会导致它。 – Loktar 2011-02-09 21:26:28

+0

如何编写路径使其工作?路径是正确的。 – frenchie 2011-02-09 21:27:08

回答

3

你需要检查你的jQuery调用到底发生了什么 - 我建议你使用firebug(或者你选择的浏览器中的等价物)来跟踪javascript并查看响应/请求。

这将允许您看到从网页/方法返回的任何错误。


更新:

如果使用dataType: "json"你应该发送正确JSON

+0

绝对没有任何反应。我尝试了更新面板中的另一个ajax调用,当它触发时,我看到了firefox中的调用。但是,当我点击触发jquery调用的按钮时,它就是nada。 – frenchie 2011-02-09 21:31:45

2

您需要在您的ajax调用中正确编码data,并引用参数名称。你需要使用带参数名称的JSON字符串作为属性,这样Asp.NET页面方法才能正确理解数据。例如:

data: "{'TheNewDate':'" + DateString +"'}", 

马修使得在评论好点 - 你需要正确的序列化这样的.Net正确理解它(所有其他原语做工精细,日期是缺乏JS文字表达的形式,并且你的约会因此有问题)。我发现a good example on the SCHOTIME.NET blog其中有一个方便的小功能序列化JS Date对象为MS兼容的JSON字符串

Date.prototype.toMSJSON = function() { 
     var date = '"\\\/Date(' + this.getTime() + ')\\\/"'; 
     return date; 
}; 

你的全ajax调用会再看看这样的:

dateValue = new Date(TheYear, TheMonth , TheDay); 

$.ajax({ 
    type: "POST", 
    url: "/Pages/CallHistory.aspx/ResetDate", 
    contentType: "application/json; charset=utf-8", 
    data: "{'TheNewDate':" + dateValue.toMSJSON +"}", //<-- the modification 
    dataType: "json", 
    success: successFn, 
    error: errorFn 
})