0

I know this call works, because I can see it in the xhr headers when debugging. However, the data never seems to hit the controller and I can't figure out why. My ASP.Net skills are pretty limited so I'll post everything I've done.

JS:

var data = JSON.stringify(myObj)
$.ajax({
    url: '/Things/UploadInfo',
    type: 'POST',
    data: data,
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    crossDomain: true,
    cache: false,
    processData: false,
    success: function (data) {
      console.log('success')
    },
    error: function() {
      console.log('error')
    }
});

ThingsController.cs

string data keeps returning null

[System.Web.Http.HttpPost]
public JsonResult UploadInfo(string data)
{
    InfoModel someInfo = new InfoModel();

    return Json(new { status = "success" });
}

InfoModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectName.Models
{
    public class InfoModel
    {
        public class Attr
        {
            public string name { get; set; }
            public string age { get; set; }
            public string sex { get; set; }
        }
    }
}
3
  • 1
    "Content" is (when using the default template) a folder where your images, CSS, etc. lives. Did you rename that folder? If not, it normally won't get mapped by the routing engine. Commented Mar 3, 2017 at 22:24
  • Where does that ajax call live? Are you using a script tag within a view? Commented Mar 3, 2017 at 22:58
  • It's within a Vue.js build. The built files are within a view. Commented Mar 3, 2017 at 23:01

2 Answers 2

1

I would start by simplifying your ajax call. The setup you're using is required for WebForms, but the MVC and WebApi frameworks don't require those extra options (with the exception of crossDomain, but it doesn't look like you're actually doing anything CORS-related).

Here's what I would use:

$.post('/Things/UploadInfo', myObj)
    .done(function (response, status, jqxhr) {
      console.log('success')
    })
    .fail(function(jqxhr, status, error) {
        console.log('error')
    });

This assumes that myObj is either a JSON object or can be implicitly serialized.

Your UploadInfo action should probably have either a model to bind, or the individual parameters you're expecting. Let's say you had this model:

public class Foo
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Then your action would look like

public ActionResult UploadInfo(Foo model)

or

public ActionResult UploadInfo(int id, string name)

and both would be mapped by the model binder. As a side note, you can leave the return type (mostly) generic - JsonResult inherits ActionResult, so you can still use return Json() when the return type is ActionResult.

If you're expecting varying JSON objects, then the data object you supply to the ajax call would be something like:

var data = {
    data: myObj
};
Sign up to request clarification or add additional context in comments.

Comments

0

A few things:

  1. For your ajax url, use "http://localhost:portNumber/Things/UploadInfo"

  2. Change your UploadInfo parameter to (InfoModel data).

  3. Use a utility like Chrome extension Advanced Rest Client and make sure you can hit a breakpoint in your method. https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en-US

2 Comments

Thanks for the info. I changed my ajax url and the parameter info, but I'm getting the same results. ARC is giving a 200 OK status as well.
Can you set a breakpoint in your method when testing with ARC? When/if it broke into your method, was the parameter data null? Empty?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.