简体   繁体   中英

Simple Ajax Call to MVC method always returns error

I have a very simple ajax call to an MVC method and it always return an error with a blank responseText. I have called and used ajax for many years and cannot seem to figure what is causing this:

here is the js code:

 $.ajax({
    url: "/Mobile/MyMVCMethod",
    data: null,
    type: "POST",
    dataType: 'html',
    success: function (data) {
        alert("test");
    },
    error: function (request, status, error) {
        alert(status);
        alert(request.responseText);
        alert(error);
    }
});

here is my C#:

[HttpPost]
public string MyMVCMethod()
{
    return "test";
}

the code makes it to method but return an error with no details

I have tried ActionResult because I wanted to return a PartialView but since it was not working I tried to narrow it down and find out if will work with just a string. THis is happening even if the C# was this:

[HttpPost]
public ActionResult MyMVCMethod()
{
    return PartialView("MyView");
}

MyView:

<div>hello world</div>

Now I see an error in Ajax " Resource Not Found" even though it executed the MVC method

$.ajax({
        url:"Mobile/MyMethod",
        method:"post", 
         datatype:"json"
}).done(function( data ) {
    alert( "Data " + data );
  });

and make sure call only normal method not action result.then try it may it will work for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM