I'm making a litle test for a Web Api project before developing a real App. I implemented some basic GET action without a problem, but I have some issues with the POST request. I'm calling the POST request with jQuery like this

var data = { name: "Pedro" };
$.post({
        url: "http://localhost/Demo/api/user",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: $.stringify("="+data),
        success: function (data) {
            alert(data[0].Name);
        },
        error: function () {
            alert("error");
        },
        complete: function () {
            alert("complete");
        }
    })

And my POST action is like this (on UserController.vb)

Public Function PostUser(<FromBody()> ByVal name As String) As Demo.User.Model.User
    Dim ids As Integer() = From userId In (From user In users Select user.Id)

    Dim maxId As Integer = ids.Max + 1
    Dim newUser As Demo.User.Model.User = New Demo.User.Model.User With {
        .Id = maxId, _
        .Mail = name & "@abc.com", _
        .Name = name _
    }
    users(maxId) = newUser
    Return newUser
End Function

With the above jQuery code I receive no error or exception message, and the action it isn't even reached. However, If I test the action from the codebehind (I'm implementing the Web Api through a Web form project) the action get called. Here is how I test from the codebehind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim client As New WebClient()
    Dim data As New NameValueCollection()

    data.Add("name", "Pedro")

    Dim response As Byte() = client.UploadValues("http://localhost/Demo/api/user", "POST", data)
    lbl.Text = Encoding.ASCII.GetString(response)
End Sub 

So, my question is, why I can't reach the web Api action with my js code?

Befor you ask, I use $.stringify("="+data), because I extended jQuery with this code, I've done that because I need to support old versions of IE.

I also checked this question, with no success

UPDATE: I have debugged the site on Chrome and I get an 404 error. For some reason, the data object is being sended like this http://localhost/Demo/[object%20Object]

Related posts

Recent Viewed