I am relatively new to AJAX and JavaScript, so I appreciate your patience. My goal is to use an AJAX call to send a new user object to a WCF C# method, which will then create the user in a SQL server database. Subsequently, the C# method will send back a validation object to the AJAX call containing a message confirming the success or failure of the user creation process.
Here is the AJAX code I am using:
$("#createUserCallButton").click(function () {
var reply = $.ajax({
url: "http://localhost/userservice/user.svc/createuser",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST",
data: JSON.stringify({
"username": "ThisIsMyUsername",
"password": "ThisIsMyPassword"
}),
success: function (data) {
console.log('success');
console.log(reply.responseText);
},
error: function (error) {
console.log('failure');
console.log(error);
},
});
});
This is the C# code:
public class User
{
[DataMember]
public string username { get; set; }
[DataMember]
public string password { get; set; }
}
[OperationContract]
[WebInvoke(Method = "*", UriTemplate = "/createuser",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
Validation Create_User(User json);
public Validation Create_User(User user)
{
Validation validation = new Validation { PassesValidation = true, Reason = ("") };
dataAccessor.CreateOne_User(user);
return validation;
}
Unfortunately, I am facing an issue where the AJAX call returns a failure with a status of 400 (Bad Request) when the user creation process is in progress, even though the user is successfully created in the database. I believe this issue stems from my understanding gap in AJAX. Despite extensive research and watching numerous tutorial videos, I have been unable to resolve this issue. I would greatly appreciate any help or solution that you can provide.
Thank you!