Jquery ajax 'post' call

I am new to jQuery and Ajax and am having problems with the "message".

I am using the jQuery Ajax 'post' call to save data to the database. When I try to save the data, it passes null to my C # method. JQuery looks like this:

function saveInfo(id) {
        var userID = id; 
        var userEmail = $('.userEmail').val();
        var userName  = $('.userName').val();

        var dataJSON = {"userID": userID, "userEmail": userEmail, "userName": userName};

            $.ajax({
                type: 'POST',
                url: '../../Services/AjaxServices.svc/SaveUser',
                data:JSON.stringify(dataJSON),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json'
            });
        return false;
    }`

.userEmail and .userName are links to input field classes. C # code is as follows:

[ServiceContract(Namespace = "http://testUsePage.com")]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]                                                                        

public class AjaxServices
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat           = WebMessageFormat.Json)]
    public void SaveUser(User user)
    {
        //code here handles save
    }
}

I have a breakpoint inside the "SaveUser" method, and the passed User object is always zero. Thank!

EDIT: "POST" "GET" ajax, WebInvoke. ({ "UserID": UserID}) (public void SaveUser ( UserID)) . .

+5
5

:

{user: {userID: 1,...}}

wrappedrequest. json .

. stringify.

+5

json - .

function saveInfo(id) {
    var userID = id; 
    var userEmail = $('.userEmail').val();
    var userName  = $('.userName').val();
        $.ajax({type: 'POST',
            url: '../../Services/AjaxServices.svc/SaveUser/?userID='+userID+"&userEmail="+userEmail+"&userName="+userName,
        });
    return false;
}
+2

RequestFormat WebInvoke json.

$.ajax send data: dataJSON JSON.stringify.

, , ( ), json.

$.post('../../Services/AjaxServices.svc/SaveUser',
 {"userId": userID, "userEmail": userEmail, "userName": userName}
);

[WebInvoke(Method = "POST", UriTemplate = "SaveUser?userId={userId}&userEmail={userEmail}&userName={userName}")]
public void SaveUser(string userId, string userEmail, string userName)
+2

Net Firefox Firebug , , -? , , , Javascript - , Javascript.

, , , - , . - , :

public void SaveUser(string userID, string userEmail, string userName)

I read your comments, try the following:

There is no script on your client side JSON object

public class AjaxServices
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public void SaveUser(string userID, string userEmail, string userName)
    {
        //code here handles save
    }
}
0
source

Simple: an attribute data $.ajax()is a map (object) of data, not a row of data. Do not reinstall the object before sending it:

var dataJSON = {"userID": userID, "userEmail": userEmail, "userName": userName};

$.ajax({
    type: 'POST',
    url: '../../Services/AjaxServices.svc/SaveUser',
    data: dataJSON,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json'
});
0
source

All Articles