2

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;
Liam
  • 27,717
  • 28
  • 128
  • 190
Richard
  • 14,798
  • 21
  • 70
  • 103
  • How does it *arrive*? What is processing the data on the server side? the default for that form getting posted in the way you've described would be `token=a987a87f9k`. I.e. standard url encoding. Not `token:a987a87f9k` – Liam May 26 '15 at 08:47
  • On the server the content is the string "token:a987a87f9k", read like so: var content = request.Content.ReadAsStringAsync().Result; – Richard May 26 '15 at 08:58
  • So your using Web API? What content-type are you sending your HTTP request with? – Liam May 26 '15 at 09:01
  • Yes. And all the JS that I have is in the question. – Richard May 26 '15 at 09:05
  • 1
    Your correct to say `token:a987a87f9k` is invalid JSON, but so is `'token':'a987a87f9k'`. A valid JSON object would be `{'token':'a987a87f9k'}`. If your not sending the content type as JSON then it should be in a standard name-value pair format (as I said above). Now I don't know Web API so someone else will have to help here, but something about this doesn't really make sense. There is nothing wrong with the JavaScript, the issue lies server side. – Liam May 26 '15 at 09:10
  • Ok, if this is how the data is supposed to arrive then I'll just handle it using primitive string operations. Not the best solution but it's only one method in my whole app. Thanks. – Richard May 26 '15 at 09:43
  • I'm not saying this is the format it should be in. TBH I don't know. What I am saying is that it isn't JSON and it's not `name=value` which is the default for HTTP posts. So I don't know what it is. – Liam May 26 '15 at 09:48

1 Answers1

0

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

Community
  • 1
  • 1
pinturic
  • 2,253
  • 1
  • 17
  • 34
  • 1
    `name:value` is not form encoded. Form encoded is `name=value`. [See here](http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data) – Liam May 26 '15 at 10:22
  • Yes you are right I did not see it correctly. Thanks – pinturic May 26 '15 at 10:29