0

I am writing an Asp.net MVC application and I have an ajax call:

    var email = $("#emailInput").val();
    alert(email);
          $.ajax({
            type: "POST",
            url: "/Home/Validate",
            data:email,
            dataType:'text',
            success: function (data) {
                alert(data);
            },
            error: function () {
                alert("error");
            }
        });

My controller:

[HttpPost]
public string Validate(string email)
{

}

The parameter email is always null. Any ideas? I've tried sending the email string as a json object, but I've got the same result.

2 Answers 2

2

See below:

$.ajax({
    type: "POST",
    url: "/Home/Validate",
    data: { email: email },
    success: function (data) {
        alert(data);
    },
    error: function () {
        alert("error");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You need to send the key and value pair for this to work.

Something like this. var dataPair = {"email": email parameter value here};

url: "/Home/Validate", data:dataPair,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.