经过深思熟虑,我发现了问题所在。我正在使用jquery's editable plugin。这是问题的根源。当 jeditable 调用 jquery 的 ajax 时,它会设置这样的 ajax 选项:
var ajaxoptions = {
type: 'POST',
data: submitdata,
url: settings.target,
success: function(result, status) {
if (ajaxoptions.dataType == 'html') {
$(self).html(result);
}
self.editing = false;
callback.apply(self, [result, settings]);
if (!$.trim($(self).html())) {
$(self).html(settings.placeholder);
}
},
error: function(xhr, status, error) {
onerror.apply(form, [settings, self, xhr]);
}
};
所以它以简单的 html 形式发送内容,并且要将其与页面方法一起使用,我们需要设置这些内容以使其以 json 形式发送。所以我们需要像这样在设置中添加一些东西:
var ajaxoptions = {
type: 'POST',
data: submitdata,
url: settings.target,
dataType: 'json', //Data Type
contentType: 'application/json; charset=utf-8', //Content Type
//....other settings
};
所以我在设置 dataType 和 contentType 中添加了两个新属性,并将上面的更改为:
var ajaxoptions = {
type: 'POST',
data: submitdata,
url: settings.target,
dataType: settings.dataType,
contentType: settings.contentType,
//....other settings
};
现在出现了另一个问题:(它正在发送数据(来自 submitdata 属性)作为 asp.net 不接受 json 请求的普通查询字符串。所以我必须使用 jquery's json plugin 并使用以下对 dataType 的测试来更改在 ajax 中发送数据的方式:
if (settings.dataType == "json") {
if ($.toJSON) {
submitdata = $.toJSON(submitdata);
}
}
它就像微风一样工作!!!