Click here to Skip to main content
15,916,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code

<script>
    $('#btnLogin').click(function () {

        var body = {'grant_type': 'password','Email':$('#Email').val(),'password': $('#Password').val()};

        alert(JSON.stringify(body));

        $.ajax({
            url: 'http://localhost:46382/Token',
            method: 'POST',
            ContentType: 'application/x-www-form-urlencoded',
            data: body,
            success: function (d) {
                console.log(d);
                alert("Login successfully Successfully");
                sessionStorage.setItem("accessToken", response.access_token);
                window.location.href = 'home';
            },
            error: function (error) {
                alert("Error ");
            }
        });
    });
</script>


and rest all the configuration in web api is default

What I have tried:

I have also tried changing the content type to application/json
and I have added
<add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
      <add name="Access-Control-Allow-Credentials" value="true" />

in web config
And it is working when posting from code behind,
but I want do this by json
Posted
Updated 3-Nov-17 1:46am

1 solution

500 errors mean the code on the server crashed or failed for some reason.

That could be because you sent parameters it didn't expect, sent them in a format it didn't expect, values out of range, or the code on the server is poorly written and doesn't check the input parameters before trying to process them, or some combination of the above.
 
Share this answer
 
v2
Comments
manish.communityhub 3-Nov-17 8:32am    
But it is working from controller here is the code
HttpContent requestContent = new StringContent("grant_type=password&username=" + Username + "&password=" + Password, Encoding.UTF8, "application/x-www-form-urlencoded");
Dave Kreskowiak 3-Nov-17 10:10am    
First, that has nothing to do with the problem.

Second, you're sending passwords between the client and the controller IN CLEAR TEXT! Could you make this thing any less secure?

In your code you posted above, you're creating a JSON object but you're not enclosing the email and password strings in single quotes. This is the offending line:

{'grant_type': 'password','Email':$('#Email').val(),'password': $('#Password').val()}

If should be closer to this:
{'grant_type': 'password','Email':'$('#Email').val()','password': '$('#Password').val()'}
Notice the quotes?
manish.communityhub 3-Nov-17 10:41am    
I have changed the json
{ 'grant_type': 'password', 'Email': "'" + $('#Email').val() + "'", 'password': "'" + $('#Password').val()+"'" };
But this time it is showing
{"error":"unsupported_grant_type"}
Dave Kreskowiak 3-Nov-17 15:19pm    
I couldn't tell you because nobody has any clue what the code looks like server-side that returned that response.
Richard Deeming 6-Nov-17 14:26pm    
You don't want those single quotes in there! :)

You want the properties of the object to be set to the values returned from the .val() function, not the literal strings.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900