2

I have read every post here about it and didn't find the answer to fix it.

I have an action in this controller:

public class AccountController : Controller
{
    [HttpPost]
    public JsonResult CheckUsername(string i_username)
    {
        var MembershipProvider = new OtakimMembershipProvider();

        return Json(MembershipProvider.CheckUsername(i_username));
    }
}

This is the code in the View:

$.ajax({
    type: "POST",
    url: '@Url.Action("CheckUsername", "Account")',
    contentType: "application/json; charset=utf-8",
    data: { 'i_username': 'silagy' },
    dataType: "json",
    success: function (data) {
        alert(data);
    },
    error: function () {

    }
});

Now I keep getting this error:

POST http://localhost:61035/Account/CheckUsername 500 (Internal Server Error) 

According to posts here, I am using this to generate the URL:

url: '@Url.Action("CheckUsername", "Account")', 

Restitution - EDIT


OK, after digging in in debug mode i found the problem.

The error was: "Invalid JSON primitive: i_username."

and based on this post "Invalid JSON primitive" in Ajax processing

i changed my code to this:

var data = { "i_username": "silagy" };

            $.ajax({
                url: '@Url.Action("CheckUsername", "account")',
                type: 'POST',
                data: JSON.stringify(data),
                datatype: "json",
                contentType: "application/json; charset=utf-8",
                error: function (xhr) {
                    alert('Error: ' + xhr.statusText);
                },
                success: function (result) {
                    alert(result);
                }
            });
Community
  • 1
  • 1
Silagy
  • 3,053
  • 2
  • 27
  • 39
  • 2
    No i didn't but why should i add static, the controller is not a static class – Silagy Aug 29 '14 at 19:06
  • 1
    You're getting a 500 error, not a 404. What's the error message you're receiving? – rossisdead Aug 29 '14 at 19:08
  • 1
    So which one is it: 404 or 500? It's really not the same thing... Add a break point and see the action getting hit. Afterwards just debug your `OtakimMembershipProvider`. – Andrei V Aug 29 '14 at 19:08
  • 1
    This is the problem the action didn't hit. i have added a break point to the line "var MembershipProvider..." – Silagy Aug 29 '14 at 19:15
  • 1
    Did you change your configurations? By that I mean routes, mainly. Your code looks ok. You can also check if the `Url.Action` worked, by inspecting your view or by setting a `debugger;` just before your `$.ajax` and opening the developer console in your browser. – Andrei V Aug 29 '14 at 19:18
  • 1
    @AndreiV No, i haven't – Silagy Aug 29 '14 at 19:19

1 Answers1

3

You should json stringify your data if you use contentType = application/json.

Try following code:

Variant 1:

$.ajax({
    type: "POST",
    url: '@Url.Action("CheckUsername", "Account")',
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ 'i_username': 'silagy' }),
    // ... 
});

Or change contentType to application/x-www-form-urlencoded; charset=UTF-8. (actually this is the default option for jQuery ajax contentType so just removing application/json will solve the problem)

Variant 2:

$.ajax({
    type: "POST",
    url: '@Url.Action("CheckUsername", "Account")',
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    data: { 'i_username': 'silagy' },
    // ... 
});
Viktor Bahtev
  • 4,858
  • 2
  • 31
  • 40