简体   繁体   中英

How to make form.submit in Javascript quote delimit names and values of post data

I need to submit a programmatically created form using plain Javascript (no JQuery).

Everything works, except the post data arrives on the server without quotes (which the C# JsonConvert.DeserializeObject() method can't parse).

It arrives as token:a987a87f9k

But I want the data to arrive as 'token':'a987a87f9k'

What should I change in this code?

var form = document.createElement("form");
form.setAttribute("id", "noid");
form.setAttribute("method", "post");
form.setAttribute("action", url);
form.setAttribute("target", tabName);
form.setAttribute("style", "display: none;");

var field = document.createElement("input");
field.setAttribute("name", 'token');
field.setAttribute("value", AccountService.GetAuthenticationToken());
form.appendChild(field);

document.body.appendChild(form);
window.open('about:blank', tabName);
form.submit();

Read server-side:

//content = "token:a987a87f9k";
var content = request.Content.ReadAsStringAsync().Result;

The form post is sending the data to the server in a "form encoded format" while the server is expecting JSON. Either you change the settings on the server or you uses some technique like this one on the client: Convert form data to JavaScript object with jQuery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM