7

I'm working on an app that uses ASP.NET MVC 4. In some ways, I feel like I'm learning everything from scratch :). I'm told its worth it.

I need to post some JSON to an Action in my controller. My action looks like the following:

[Authorize]
public class MyController : Controller
{
    [HttpPost]
    public ActionResult RemoveItem(string itemID)
    {
      // Do stuff...
      return Json(new { Status = 1, Message="Success" });
    }
}

My JQuery code looks like the following:

function removeItem(id) {
  var json = { "itemID": id };
  $.ajax({
    type: "POST",
    url: "/myController/removeItem",
    contentType: "application/json; charset=utf-8",
    data: json,
    dataType: "json",
    success: removeItemCompleted,
    error: removeItemFailed
  });
}

function removeItemCompleted(results) {
}

function removeItemFailed(request, status, error) {
}

In Fiddler, I notice a 500 error is returned. The TITLE field in the response says: "Invalid JSON primitive: itemID".

What am I doing wrong?

Thank you!

6
  • What does your json variable's value look like? Commented Feb 7, 2013 at 13:43
  • 1
    If that's all you're passing there's really no need to create the variable. Your method expects a string named itemID so you should be doing something like: data: { itemID: id } No quotes around itemID Commented Feb 7, 2013 at 13:46
  • @MarcusRecck without the double quotes it's not valid JSON. Commented Feb 7, 2013 at 13:49
  • I'd like to learn the JSON approach. I actually need to pass more data. I was trying to trim it down for the sake of example. All of my data is key/value pairs though and no nested arrays or anything like that. That's why I want to use JSON. Commented Feb 7, 2013 at 13:49
  • @BillJones - Understood-- but in this case, there is no need to pass anything other than itemId. If your Action expected an object, then you could send a json stringified object over the wire. Commented Feb 7, 2013 at 13:51

4 Answers 4

11

Be sure to send JSON:

data: json,

should be

data: JSON.stringify(json),

IE7 and below needs a shim: https://github.com/douglascrockford/JSON-js

NOTE: Dave A's answer is also correct, but doesn't directly answer your issue. I +1'ed it.

Sign up to request clarification or add additional context in comments.

1 Comment

thanx @Joe, haters be everywhere
5

you don't seem to need JSON here. Ideally, the id param would be passed in your URI:

 url: "/myController/removeItem/"+id

That is probably why your Action can't be identified. It requires a parameter.

Followup: Critics who point out that data passed is a string and therefor cannot be passed as an id are incorrect. I should have pointed out that the action method should be re-written to accept string id.

5 Comments

Dave please check his controllers action. He mentioned it as string and not as integer. By appending id to url it cannot be done AFAIK
ahhh yes, I was a little sloppy. great excuse for hating ;) It really should be passed as an id. String or not.
as a follow up @Karthik you're completely wrong that an id can't be a string.
i'm sorry if i'm wrong with my comment. Can it be passed if a string is passed instead of an int ?. I thought if it was string then only strings can be passed to it. Thanks for the follow up
Also am not your hater Dave. I find many of your posts on SO to be useful. My bad i couldn't much when commenting out
4
          function removeItem(id) {
  var json = { "itemID": id };
  $.ajax({
    type: "POST",
    url: "/myController/removeItem",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(json),
    dataType: "json",
    success: removeItemCompleted,
    error: removeItemFailed
  });
}

function removeItemCompleted(results) {
}

function removeItemFailed(request, status, error) {
}

Comments

0

use this code :

$('#delete').click(function () {
        var APIURL = "/api/products";
        var id = $('#SearchText').val();
        $.ajax({
            type: "DELETE",
            url: APIURL + '/' + id,
            success: function (data) {
                alert('Employee deleted');
            }
        });
    });

Comments

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.