1

I built a website using ASP.NET MVC and Knockoutjs. This is hosted on IIS on Windows Sever 2012. There is another web application where I use ajax to POST and that application does not give any errors. The Submit button on the website is binded using the click bind to this function. This works when I run it in Debug mode on the localhost.

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);
            }
        }
    });

In the home controller I have these two web methods.

[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;
    }

I receive Internal Sever Error 500 from the Ajax request when I click on the Submit button.

But if I do this http://xxx.xxx.xxx.xxx/Home/Test I get the json result back {"Name":"Apple","Price":"3.99M"}.

Response header from Fiddler

HTTP/1.1 500 Internal Server Error Cache-Control: private Content-Type: text/html; charset=utf-8 Server: Microsoft-IIS/8.0 X-AspNetMvc-Version: 4.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Thu, 09 Jan 2014 16:22:16 GMT Content-Length: 276

After turning on customer errors and detailed error message I am receiving this message.

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

I downloaded the Mysql connector from http://dev.mysql.com/downloads/connector/net/ and I added the assembly by right clicking on References > Extensions and selecting the 4.0 version. I also tried the 4.5.

shresthaal
  • 675
  • 4
  • 13
  • 28
  • keep 'evaluation' outside JSON.Stringyfy() and check – Arpit Jan 09 '14 at 16:35
  • `data: JSON.stringify({ evaluation: ko.toJSON(self) }),` - You are double-encoding your JSON. Are you sure you want that? Use `data: { evaluation: ko.toJS(self) }`. You don't need to do any JSON encoding at all, jQuery does that for you. – Tomalak Jan 09 '14 at 16:35
  • See [this question](http://stackoverflow.com/questions/5385714/deploying-website-500-internal-server-error) to discover the underlying error. – CodeCaster Jan 09 '14 at 16:41
  • CodeCaster thank you. – shresthaal Jan 09 '14 at 16:58
  • Tomalak, I changed what you suggested but I got an error. – shresthaal Jan 09 '14 at 16:59

1 Answers1

1

Uninstalled 6.8 and downloaded 6.7. Works now.

shresthaal
  • 675
  • 4
  • 13
  • 28