1

this is my code for the web api

 // POST api/Values
    [HttpPost]
    public async Task Post()
    {
        string user = new StreamReader(Request.Body).ReadToEnd();
        var cmd = new SqlCommand(
                @"insert into Users
                select *
                from OPENJSON(@user)
                WITH( Username, Password, Email, Nickname, IsActive, Activation)");
        cmd.Parameters.AddWithValue("user", user);
        await SqlCommand.ExecuteNonQuery(cmd);
    }

and this is my code for angular js

data = JSON.stringify({Username: user, Password: pwd, Nickname: user, IsActive: '1', Activation: '2u4bubdub32939342'}); 
        $http.post('http://localhost:7792/api/values', data)
        .success(function(data, status, headers, config) {
            alert("success");
        })
        .error(function(data, status, headers, config) {
            alert( "failure message: " + data + " " +status+ " " +headers+ " " +config);
        });
        } catch (e) { alert(e.message); }

when i run it, i get the failure message. i already have COR enabled in the web apo

7
  • which angular version are u using Commented May 10, 2017 at 13:03
  • version 1, the get request works. only the post Commented May 10, 2017 at 13:06
  • What is the error message? Also, your comment seems to say the url should be api/todo, but your code is calling api/values. Commented May 10, 2017 at 13:21
  • oh sorry that was a mistake. ive updated the code Commented May 10, 2017 at 13:22
  • POST localhost:7792/api/values 500 (Internal Server Error) VM864 signup:1 XMLHttpRequest cannot load localhost:7792/api/values. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8100' is therefore not allowed access. The response had HTTP status code 500. Commented May 10, 2017 at 13:25

2 Answers 2

3

Your POST method should take a parameter, the user model, that you are correctly sending through Angular. I hope that this example can help you.

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

Comments

0

after googling the web i added into my ConfigureServices(IServiceCollection services) in startup.cs

        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
        });

and i also added into Configure(IApplicationBuilder app, .... in startup.cs

        app.UseCors("CorsPolicy");

5 Comments

Does it actually work now? Your posted API method is not taking any input, which seems... odd.
no it doesnt it connects but doesnt insert anything
Your Post method should take a parameter, the user model, that you are correctly sending through Angular. I hope that this example can help you: andrewlock.net/model-binding-json-posts-in-asp-net-core
i still dont get it
Follow some basic tutorials for ASP.NET Core Web Api then

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.