我用jQuery创建了一个.NET 2.0应用程序。但是,当我将它部署到没有安装.NET 3.5的服务器上时,它就无法工作。我感觉没有错误,也不知道如何调试它。我使用的是jQuery1.3.2.min.js。此外,在我的测试环境和安装了3.5的另一台服务器上,其工作得非常完美,但是一旦用2.0上传到生产服务器,ASP.NET中的每个回调都会失败。
<script type="text/javascript">
$(document).ready(function() {
var item = $("[id$='txtItemName']");
var category = $("[id$='ddlCategories']");
var record = $("[id$='txtRecordID']");
$("#btnSave").click(function() {
if (item.val().length == 0) {
alert("Please enter item name first.");
return false;
}
if (category.val().length == 0) {
alert("Please select a category.");
return false;
}
var paramArray = ["testText", escape(item.val()), "categoryID", category.val(), "recordID", 1];
PageMethod("SaveMyData", paramArray, AjaxSucceeded, AjaxFailed);
});
});
function AjaxSucceeded (result)
{
alert("lykkedes" + result);
}
function AjaxFailed(result)
{
alert("failed" + result);
}
function PageMethod(fn, paramArray, successFn, errorFn) {
var pagePath = window.location.pathname;
//Create list of parameters in the form:
//{"paramName1":"paramValue1","paramName2":"paramValue2"}
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
//Call the page method
$.ajax({
type: "POST",
url: "DataProcessor.aspx?" + fn + "=1",
contentType: "application/json; charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
})
;}
</script>
DataProcessor过程如下所示:
public void SaveMyData()
{
System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string line = "";
line = sr.ReadToEnd();
JObject jo = JObject.Parse(line);
string temp = (string)jo["recordID"];
Response.Write(temp);
}
我还尝试输入了AjaxFailed(result)......
任何建议都将不甚感激!
The problem may be the JSON serializer, does your server have the ASP.Net 2.0 AJAX Extensions installed?
If you can't debug, because its on the production enviroment, then are you able to insert diagnostics?
Are you hitting the AjaxFailed function, or not at all?
A first suggestion would be to write simple alert() statements at code intervals to see if you can trace what is happenening. You may want to look at the httpresult status and the httpresult responseText that is being returned from the call.
On the server side, can you write out to some log file values at specific stages, i.e. the JSON string.
Would also echo the above with regard to the serialiser. Try using
using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
MyCustomClass mcc = jss.Deserialize<MyCustomClass>(someJSONstring);
string someOtherJSONString = jss.Serialize(mcc);
I am not proud to answer my own question here, because it is truely a noob mistake I made...
The JSON DLL I used was not the one with .NET 2.0 SUPPORT. I have somehow ignored the fact and it worked on my test environment with 3.5 installed.
I Downloaded the latest version of the JSON.net dll and used the 2.0 DLL and everything worked out like a charm.
Thanks for the contributions in here.