我有一个generic-http-handler
,我从jQuery调用它
我的处理程序只返回insert values in database
,但不返回任何内容
我按照呼叫handler
function InsertAnswerLog(url) {
$.ajax({
type: "POST",
url: "../Services/Handler.ashx",
data: { 'Url': url, 'LogType': "logtype" },
success: function (data) {
},
error: function (Error) {
}
});
}
一切对我来说都很好。
但是,这是将值发布到服务器的最佳方式吗
或者我可以用一种更好的方式使用它。
您发送的数据类型似乎是JSON编码的。请在发送之前尝试以这种形式序列化数据,然后在服务器端,您应该在发送回之前对数据进行编码。
在发送到服务器之前进行序列化
function InsertAnswerLog(url) {
var DatatoSend = { 'Url': url, 'LogType': "logtype" } ;
$.ajax({
type: "POST",
url: "../Services/Handler.ashx",
data: {Jsondata: JSON.stringify(DatatoSend)},
success: function (data) {
},
error: function (Error) {
}
});
}
现在在服务器端scipt
// NB: i use PHP not asp.net but it think it should be something like
Json.decode(Jsondata);
// do what you want to do with the data
// to send response back to page
Json.encode(Resonponse);
// then you could in php echo or equivalent in asp send out the data
重要的是,您要在服务器端脚本上解码json数据,当要发送响应时,应该将其编码回json形式,以便将其理解为返回的json数据。我希望这能有所帮助。