0

I'm starting with web api. I'm trying to do a POST call.

I have my controller:

    [HttpPost]
    public myData test(myData m)
    {
        return m;
    }

I created the class myData inside models. BTW, should it be inside Views/ModelView?

public class myData
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Than, I have an HTML file that calls this POST:

    <html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>

<body>
    <script>
        var model = {
            Name: "Shyju",
            Id: 123
        };

        $.ajax({
            type: "POST",
            dataType: "jsonp",
            data: JSON.stringify(model),
            url: "http://localhost:52884/api/contact",
            contentType: "application/json"
        }).done(function(res) { 
            console.debug(res);
            // Do something with the result :)
        }).fail(function(error)  {
            console.debug(error);
        });
    </script>
</body>

</html>

*I edited the html. The Cross-Origin error was fixed with dataType: "jsonp".

My problem now is that it goes to the fail() method from AJAX, but the console returns:

status: 200
statusText: success 
readyState: 4

It should return the data from my var model. Right?

Thanks

user6824563
  • 735
  • 6
  • 25
  • 47
  • Once you get past the CORS issue you will need to alter your post so that data is assigned `data: JSON.stringify(model)` in your `ajax` call. – Igor Oct 12 '17 at 18:52
  • It says that happens when try access another domain's resources; however, I'm using my localhost – user6824563 Oct 12 '17 at 19:03

0 Answers0