When sending jQuery message to MVC, getting 404 error

I am sending from a view using jQuery to send an MVC message

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 get 404 error.

+4
source share
6 answers

You did not say which version of jquery you are using. Check the jquery version and in case this version is <1.9.0 you should instead

method: "POST" 

using

type: "POST"

this is an alias for the method, and according to the official jquery documentation you should use a type if you are using jQuery versions up to 1.9 0.0.

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

It is tested on the code and it works (each request is included in the http post SomeAction mvc controller).

+3
source

RFC 2616 404 , , -URI.

, URL. MVC :

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

404:

  • . / , .

    - , , , , :

    url: "SomeAction",

    - , , OtherController, :

    url: 'Other/SomeAction',

    - , ajax ( javascript), ( SomeController):

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

:

  • json (contentType , ):

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

  • , , "" - . , "json", .

  • JSON.stringify(: {id: passId}),

, , : DoSomething (passId) {

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

, .

/MyController/SomeAction/

URL ..

'MyController/SomeAction/ajax.php'

URL

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

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

, , ... OP : " 404".

  • contentType - , , /JSON; charset = utf-8 , /-WWW--urlencoded; charset = UTF-8, .

  • dataType - , : json, html, .. jQuery , , .

0

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 parameter stringinsteadint

-1
source

Enter the code as follows:

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 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, make sure the controller has the appropriate view. :) and the jQuery library has also been updated. just in case.

-1
source

All Articles