-1

I am trying to send my email and password to my home Controller jsonresult method. values of email and password are displayed in first alert but when i am passing userCredential to data it displays alert undefined. values of email and password are not getting passed in ajax post method

    $("#button_val").click(function () {
        var userCrdential = "email=" + $("#username").val() + "&password=" 
                              + $("#pwd").val();
        alert(userCrdential);
        $.ajax({
            type: "POST",
            url: "/Home/adduser",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: userCrdential,
            success: function (res) {
                //  alert("data is posted successfully");
                if (res.success == true)
                    alert("data is posted successfully");
                else {
                   // alert("Something went wrong. Please retry!");
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                alert(xhr.statusMessage);
            }
        });
    });.

controller

[HttpGet] public JsonResult adduser(user obj) { }
sonali
  • 3
  • 4
  • 1
    What is the signature for you MVC controller method? – Steve Apr 02 '19 at 10:31
  • [HttpGet] public JsonResult adduser(user obj) { }//user is model where i have email and password property – sonali Apr 02 '19 at 10:38
  • 1
    The method is a GET, and you are doing a POST. Can you add this to your question. I would do a bit of research on how to do a POST in MVC correctly. – Steve Apr 02 '19 at 11:16
  • You could also re-use the method for `POST` by decorating the method with `[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]` instead of `[HttpGet]`. – user692942 Apr 02 '19 at 12:23

2 Answers2

1

By looking at your comment, the first thing that I observed is that, you are using the post method on ajax call while at the controller level, it is the http get. It could be the problem. It is good if you can put the code for your action method too.

Here is the sample example of post object through ajax in mvc,

    [HttpPost]  
    public ActionResult YourMethodName(MyViewModel myViewModel)  
    {            
     //your code goes here.
    }

You can make the ajax call like below,

   var requestData = {
     Email: 'pass the email value here',
     Password: 'pass the password value here')
  };


  $.ajax({
     url: '/en/myController/YourMethodName',
     type: 'POST',
     data: JSON.stringify(requestData),
     dataType: 'json',
     contentType: 'application/json; charset=utf-8',
     error: function (xhr) {
        alert('Error: ' + xhr.statusText);
     },
     success: function (result) {

     },
     async: true,
     processData: false
  });
0

In your controller method you use [HttpGet] but in ajax request you write POST. So, go to one option POST or GET. I write the solution with GET First change your Controller method with this:

    [HttpPost] 
    public JsonResult adduser(string email, string password) { 
    // work here
    }

Then modify your code:

$("#button_val").click(function () {
            var userCrdential = "email:"+ $("#username").val()+", password:"+ $("#pwd").val();
            alert(userCrdential);
            $.ajax({
                type: "POST",
                url: "/Home/adduser",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: {userCrdential },
                success: function (res) {
                    //  alert("data is posted successfully");
                    if (res.success == true)
                        alert("data is posted successfully");
                    else {
                       // alert("Something went wrong. Please retry!");
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(xhr.statusMessage);
                }
            });
        });
Uzzal Prasad
  • 115
  • 1
  • 11