When sending jQuery post to MVC controller getting 404 error

I'm sending from view using jQuery to MVC post action

function DoSomething(passedId) { $.ajax({ method: "POST", dataType: 'text', url: '/MyController/SomeAction/', data: { id: passedId} }).done(function (data) { // }); }

And inside MyController

[HttpPost] public ActionResult SomeAction(int id) { ... }

In Firebug console I'm getting 404 error.

--------------Solutions-------------

You didn't said which version of jquery you are using. Please check jquery version and in case that this version is < 1.9.0 you should instead of

method: "POST"

use

type: "POST"

this is an alias for method, and according to jquery official documentation you should use type if you're using versions of jQuery prior to 1.9.0.

function DoSomething(passedId) {
$.ajax({
type: "POST",
dataType: 'text',
url: '/MyController/SomeAction/',
data: { id: passedId}
}).done(function (data) {
...
});
}

Tested above code and it works (each request enter inside mvc controller http post SomeAction action).

In the RFC 2616 the code 404 indicates that the server has not found anything matching the Request-URI.

So you need to look at your URL parameter. Try the MVC conventional call using :

url: '@Url.Action("SomeAction", "MyController")',

The slash at the beginning of this designates an absolute path, not a relative one.

/MyController/SomeAction/

You should include a URL or relative path.. maybe

'MyController/SomeAction/ajax.php'

or the full URL

'http://example.com/myajaxcontroller/someaction/ajax.php'

or stolen from the other guys answer

url: '@Url.Action("SomeAction", "MyController")',

To address others on here, I don't think the datatype is the problem... OP says "I'm getting 404 error."

  • contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; charset=UTF-8, which is the default.
  • dataType is what you're expecting back from the server: json, html, text, etc. jQuery will use this to figure out how to populate the success function's parameter.

To resolve the 404 issue:

  1. There are a few options to resolve this. You controller/action cannot be find the way it is describe.

    -If you are in a view that is in the controller for which the action your are trying to call is located, then:

    url: 'SomeAction',

    -If you are trying to call an action from another controller, OtherController, for example, then:

    url: 'Other/SomeAction',

    -To add to another answer, if you are calling your ajax inside the view (and NOT in a javascript file) then you can also use (for a controller called SomeController):

    url: '@Url.Action("SomeAction", "Some")',

Additional Items Of Note:

  1. You do not specify a content type for json (contentType indicates what you are sending):

    contentType: "application/json; charset=utf-8",

  2. I can't tell, based on your action if you are expecting 'text' or something else. However, unless expecting 'json', I would remove the data part.
  3. You need to stringify your data

    JSON.stringify(data: { id: passedId}),

In the end, I would expect it to look something like: function DoSomething(passedId) {

var url = "SomeAction"; //if action is in OtherController then: "Other/SomeAction"
$.ajax({
method: "POST",
url: url,
data: JSON.stringify({ id: passedId}),
contentType: "application/json; charset=utf-8"
}).done(function (data) {
//
});
}

Write the code this way:

function DoSomething(passedId) {
$.ajax({
url: 'yourController/SomeAction',
type: 'POST',
data: { id: passedId},
dataType: 'json',
error: function (ex) {alert(ex.responseText)},
success: function (data)
{
if (data.Results != null) {
//use the return values
});
}
}
});
}

and the controller

public JsonResult SomeAction(int id)
{
try
{
return Json(new { Results = "Text To return or send some object or an list, etc"}, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
throw;
}
}

Finally, check that the controller has its respective view. :) and and the library of "jQuery" updated. just in case.

use the following ajax call

var datum = { id: passedId };
$.ajax({
url: url, // your url
type: 'POST',
data: JSON.stringify(datum),
contentType: 'application/json; charset=utf-8',
beforeSend: function () {

},
complete: function () {

},
success: function (user, status, XHR) {

},
error: function (req, status, error) {

}
});

UpDated
public ActionResult SomeAction(int id){} should accept string parameter instead of int

Category:javascript Time:2018-06-23 Views:0

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.600 (s). 13 q(s)