-1

So, some time ago, I needed to make ajax requests to access the code behind. I did that (here).

But now I'm working on asp.NET Core 2.0 and it works differently. Well, now I can make a request but, for some reason The variables are null (only on the code behind, on the client side they have content. Probably isn't a big deal, but I can't grasp why.

So, here's my ajax function:

function Ajax(expression1, expression2, url, json, error) {
            console.log(expression1+" - "+ expression2);
            var request = { email: expression1, password: expression2 }

    $.ajax({
        url: url,
        method: 'post',
        contentType: 'application/json',
        data: JSON.stringify(request),
        dataType: 'json',
        success: function (resp) {
            console.log(request);
            if (expression1 === null || expression2===null)
                window.location.href = resp[json];
            else
                document.getElementById(error).innerHTML = resp[json];
        },
        error: function (error) {
        }
    })
}

And the c# code:

public string ExternalLogin(string email, string name)
        {
            //TODO: Facebook login

            // Redirect link example. Could be another one.
            return "{\"facebook\":\"Home/About\"}";
        }

The ajax function makes the request and it triggers the c# function, and returns what I ask it to. The only problem is the null variables. Why is this happening?

EDIT: The User class as requested:

public class User
    {
        [JsonProperty(PropertyName ="email")]
        public string Email { get; set; }
        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }
        [JsonProperty(PropertyName = "password")]
        public string Password { get; set; }
    }

1 Answer 1

0

The parameter binding fails because you are making a post request here. You have to add the [FromBody] attribute to your parameters.

You can get more info about paramter binding here:

EDIT:

You also passing a object as paramter so you either have to change you route paramter to an object containing email and password property, or you change your Content-Type to 'application/x-www-form-urlencoded' and pass a query string like 'email=....&password=....'

Sign up to request clarification or add additional context in comments.

6 Comments

Just that? I've added the [FromBody] tag but it still won't receive any value.
So, I've created the object as suggested and it worked. But now I have a followup. I have a password property too. And somehow it deserialized the name to the password field. That's weird. I even added, in the user class, the tag (?) [JsonProperty("name")] for it to know where it's supposed to be, but it remains the same...
Could you plz provide you User Class?
Have you tried adding a 'name' property to your request object?
Oh, I see what's going on. I pasted the wrong ajax function here. This on is for the app login, thus the password, the other one has a name field instead. But it's basically the same, wth the exception that this one has 5 args and the other has 4. But still I call the function with 4 args and it still jumps for the one that has 5 args. Odd...
|

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.