admin管理员组

文章数量:1293466

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.

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.

Share edited May 15, 2015 at 14:31 softvar 18.5k13 gold badges57 silver badges77 bronze badges asked May 15, 2015 at 13:42 user1765862user1765862 14.2k33 gold badges135 silver badges245 bronze badges 2
  • Your dataType says 'text', is that what you are expecting back in your controller action? – JasonWilczak Commented May 15, 2015 at 13:47
  • Have you checked the rewrite config for the path you are using, is that all good? Can you post the rewrite rules? – Tech Savant Commented May 15, 2015 at 14:07
Add a ment  | 

6 Answers 6

Reset to default 3

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")',

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

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./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 mon 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.

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 () {

    },
    plete: function () {

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

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

    }
});  

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

本文标签: javascriptWhen sending jQuery post to MVC controller getting 404 errorStack Overflow