I am trying to post the data of my form which is bound to an administrator through an Ajax request However, the controller is showing that the data is empty, data is being sent even though the data header is displayed.
The code is below, I have tried the data: JSON.stringify (form) which is the result of the null model, whereas models with empty data have the following results.
See
$ (document)) .on ('click', '#saveData', function () {if ($ ('$ form'). ()) {Var form = $ ('# form'). Serialize (); $ .ajax (Url.Action ("CreateClient", "Processors"), Type: 'POST', cache: incorrect, async: false , Datatype: 'json', content type: 'application / jason', data: json.stringify (form)} .success (response) {warning (response);}). Panic (function) (warning) (reaction);});}});
Administrator
Create a public functioning client (model data form) {if (form == empty !! ModelState.IsValid) {Jason ("Error") ; } Return Jason ("success"); }
There are two problems with your point of view.
For example, if your model is a Class ModelData,
Class ModelData {public string foo {get; Set;} public string bar {get; The appropriate data for sending {foo: "foo1", bar: "bar 1"}
, or finally {Foo: set}}}
"Foo1", bar: "bar 1"} , depending on how you have configured your serialization - as you type content 'application / json'
.
However, if you are reading your form using jquery, this method returns a string on "foo = foo1 and bar = bar1"
, ContentType
is appropriate for 'application / x-www-form-urlencoded'
. So you have to do it in a format in which you want to send the data. If you want to continue using serialize () to get data from the DOM, use 'application / x-www-form-urlencoded'
instead.
Secondly, JSON will create a JSON string from an object, a string is also an object, so passing this string will wrap the string in a string string, which does not make much sense: data "\ Similar to "foo = foo1 and bar = bar1 \" "
. Similarly, the jQuery AJAX function will expect a data object parameter when contentType
is 'json', so if you convert your object before the string, it will be sent in the same way: wire. In fact, whatever contentType
you choose for your request, do not use JSON.stringify for your data parameter.
TL; DR: To make this work, use the default contentType
or explicitly declare it as below, and pass the form variable - such as:
Var form = $ ('# form'). Serialize (); $ .Ajax ({// (...) Content Type: 'app / x-www-form-urlencoded; charset = UTF-8', data: form, // (...)
Comments
Post a Comment