1

I'm getting a 500 (Internal Server Error) when I try to run

    $.ajax({
        xhr: function() {
            var xhr = new window.XMLHttpRequest();
            return xhr;
        },
        type: 'POST',
        url: '@Url.Action("MyAction","MyController")',
        data:  fi,
        contentType: "application/json; charset=utf-8",
        dataType: "json", // dataType is json format
        success: function(retJson) {
            // ...

        }
    });

where fi is a JavaScript map like {org: "string1", cat: "string2", fileName: "string3"}, and myAction is like

    [HttpPost]
    public async Task<JsonResult> myAction (FileInfo fi)
    {

where FileInfo is defined by

    public class FileInfo
    {
        public string org { get; set; }
        public string cat { get; set; }
        public string fileName { get; set; }
    }

Any idea what I'm missing here?

  • What is the 500 server error? – AmmarCSE Jun 15 '15 at 22:30
  • Whats in the stack trace of the 500 response, it should provide more detail? – Brent Mannering Jun 15 '15 at 22:30
  • The error is printed in the JavaScript console. –  Jun 15 '15 at 22:33
  • @EdwardSnowden, yes and what is it? – AmmarCSE Jun 15 '15 at 22:33
  • It's `500 (Internal Server Error)` That's the only information I have. EDIT: I see `i.ajaxTransport.send @ jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1 i.extend.ajax @ jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1 addFilesToDB @ FileUploadAsync:304 $.ajax.success @ FileUploadAsync:396 i.Callbacks.a @ jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1 i.Callbacks.h.fireWith @ jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1 k @ jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1 i.ajaxTransport.send.u @ jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1` –  Jun 15 '15 at 22:34
  • Open `F12` go to the network tab, track the request and see what the response is. it should give you a fully detailed message – AmmarCSE Jun 15 '15 at 22:35
  • It's `[ArgumentException: Invalid JSON primitive: org.]` –  Jun 15 '15 at 22:37
  • 1
    See http://stackoverflow.com/questions/2445874/invalid-json-primitive-in-ajax-processing – AmmarCSE Jun 15 '15 at 22:40
  • 1
    Remove `contentType: "application/json; charset=utf-8",` (or alternatively use `data: JSON.stringify(fi),` –  Jun 15 '15 at 22:41

1 Answers1

1

The issue is with the way your JSON object is defined.

It should have the single quotes for the prop names like this:

 {'org': 'string1', 'cat': 'string2', 'fileName': 'string3'}
Aram
  • 5,537
  • 2
  • 30
  • 41