Asp.net 500内部服务器错误ajax post json
我用ASP.NETMVC和Knockoutjs建立了一个网站。它托管在Windows Server 2012上的IIS上。还有一个web应用程序,我使用ajax发布,该应用程序不会给出任何错误。网站上的“提交”按钮是使用“单击绑定到此功能”绑定的。当我在本地主机上以调试模式运行它时,这会起作用Asp.net 500内部服务器错误ajax post json,asp.net,ajax,json,iis,knockout.js,Asp.net,Ajax,Json,Iis,Knockout.js,我用ASP.NETMVC和Knockoutjs建立了一个网站。它托管在Windows Server 2012上的IIS上。还有一个web应用程序,我使用ajax发布,该应用程序不会给出任何错误。网站上的“提交”按钮是使用“单击绑定到此功能”绑定的。当我在本地主机上以调试模式运行它时,这会起作用 self.SubmitEvaluation = function () { reqUrl = "/Home/SubmitEvaluation"; $.ajax({ type
self.SubmitEvaluation = function () {
reqUrl = "/Home/SubmitEvaluation";
$.ajax({
type: "POST",
url: reqUrl,
data: JSON.stringify({ evaluation: ko.toJSON(self) }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response == 1) {
self.EvalComplete(true);
} else {
self.EvalComplete(false);
alert("Submission Failed. Please resubmit!!!")
}
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].'+exception.toString());
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
在home controller中,我有两种web方法
[HttpGet]
public string Test()
{
Logger.Logger.WriteLog("Test", "Inside Test");
TestModel product = new TestModel();
product.Name = "Apple";
product.Price = "3.99M";
return JsonConvert.SerializeObject(product);
}
[HttpPost]
public int SubmitEvaluation(string evaluation)
{
Logger.Logger.WriteLog("Submitevaluation", "Inside SubmitEvaluation");
int result = -1;
try
{
EvaluationModel eval = JsonConvert.DeserializeObject<EvaluationModel>(evaluation);
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
string sql = "INSERT INTO xyz table blah blah blah";
MySqlCommand cmd = new MySqlCommand(sql, conn);
result = cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Logger.WriteLog("Submitevaluation", ex.ToString());
}
return result;
}
我从下载了Mysql连接器,并通过右键单击References>Extensions并选择4.0版本添加了程序集。我还尝试了4.5。卸载了6.8并下载了6.7。现在可以工作。将“evaluation”放在JSON.Stringyfy()之外,并选中
数据:JSON.stringify({evaluation:ko.toJSON(self)}),
-您对JSON进行了双重编码。你确定你想要吗?使用数据:{evaluation:ko.toJS(self)}
。你根本不需要做任何JSON编码,jQuery会帮你做。请参阅以发现潜在的错误。CodeCaster谢谢。Tomalak,我更改了你的建议,但我遇到了一个错误。
Could not load file or assembly 'MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified