If you're facing issues, try observing the POST request made using $.ajax
(using tools like Fiddler or browser developer tools). It's possible that jQuery may be sending the data as a URL-encoded string instead of JSON format.
To resolve this problem, make sure to set the dataType
and contentType
parameters. Additionally, there might not be a need for using JSON.stringify
; you can simply pass the JSON object directly:
$.ajax({
data: usr,
dataType: 'json',
contentType: 'application/json',
/* Other configurations go here. */
});
Below is an example of a TypeScript method used in one of our projects (where ko.toJSON
converts a JSON literal into a string):
public static callApi(url: string, type?: string, data?: any): RSVP.Promise {
return new RSVP.Promise((resolve, reject) => {
$.ajax('/api/' + url, {
type: type || 'get',
data: data != null ? ko.toJSON(data) : null,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: () => {
resolve.apply(this, arguments);
},
error: () => {
reject.apply(this, arguments);
}
});
});
}
Hopefully, this information proves helpful.