0

How to send JSON request parameters to server while submitting the form?

function fun2() {
alert("in fun2");
document.searchForm.action = "http://localhost:8080/DatasetRepo/rest/downloadZip?"+"{'datasets':['1'],'token':'myfirsttoken'}";
document.forms["searchForm"].submit();}      

and its failing with Unsupported media type error.

  • check this questions answer. http://stackoverflow.com/questions/19446544/post-request-to-include-content-type-and-json browsers don't support json as media type for POST. – th1rdey3 Dec 30 '14 at 16:14

6 Answers6

2
$.post( "test.php", { name: "John", time: "2pm" } ); 

or

$.get( "test.php", { name: "John", time: "2pm" } ); 
jeremyb
  • 470
  • 4
  • 12
  • The OP would need jQuery to do this... it is not a valid answer to the question. Also specified to not use $.ajax... $.post and $.get are shorthand $.ajax requests – zgr024 Dec 30 '14 at 16:43
  • Yes. zgr024 is absolutely right... Needed form submission and get the download from server. – Sureshkumar Gembali Dec 31 '14 at 05:24
0

Use serialize to get form data, like

var form=$('form[name=searchForm]');
$.post(url, form.serialize(),callback)
Tracholar Zuo
  • 467
  • 2
  • 8
  • Not looking for $.post or ajax related stuff. Have to send json and get the download from server...(if I send thru ajax, getting download is becoming difficult) – Sureshkumar Gembali Dec 31 '14 at 05:22
0

Instead of sending as json, just send as querystring...

function fun2() {
    document.searchForm.action = "http://localhost:8080/DatasetRepo/rest/downloadZip?datasets[]=1&token=myfirsttoken";
    document.forms["searchForm"].submit();
}  

Then the document you are posting to will have the values in the $_GET array...

<?php
    print_r($_GET);
?>
zgr024
  • 1,175
  • 1
  • 12
  • 26
  • Its failing with Unsupported Media type as server code in java expecting json. I have jsonMessageConverter in spring configuration. – Sureshkumar Gembali Dec 31 '14 at 05:27
  • You won't be able to do this directly from a form submission. In PHP, i use curl. In Java it's very similar. See http://stackoverflow.com/questions/2586975/how-to-use-curl-in-java for info – zgr024 Jan 01 '15 at 21:04
0

This is probably not a good approach to take, as if you want to send JSON, you will also want to set Contetn-Type header to application/json so the receiving server understands that it is not dealing with a URL-encoded query string as you would be with typical form posts. Most browsers do not yet support using form enctype="application/json" as this is still just a draft part of HTTP spec.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Great. Thanks for sharing this info. Even I tried that putting enctype="application/json" in form tag. but did not work. Do you have any suggestion for doing this? (Because I have to get a download from server, ajax requests I can't go with) – Sureshkumar Gembali Dec 31 '14 at 05:30
0

Use JSON.stringify function to send json in ajax post, below snippet can be used to send data using ajax.

var data = {
      one: 'first',
      two: 'second'
};

function formSubmit(url, data) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function () {
    ajaxCallback(xmlhttp);
  };
  xmlhttp.open("POST", url, true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  xmlhttp.send(JSON.stringify(data));
 }

 function ajaxCallback(xmlhttp) {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
       var response = JSON.parse(xmlhttp.responseText);
      console.log(response);
    }
 }

Hope this helps.

Puneet Bharti
  • 126
  • 1
  • 7
  • May be I can change Content-type to applicaiton/json as server is expecting json format. But can you suggest me to take a download from server as application/octet-stream by using this xmlhttp object or somehow? – Sureshkumar Gembali Dec 31 '14 at 05:33
  • Yes, Change content-type to application/json or convert json to query string to send data. I don't think you can download any file using xmlhttp object instead you can get a whole stream of a file as a string in json that could be processed later. – Puneet Bharti Dec 31 '14 at 05:59
0

You will need to post the data to a PHP script and use curl to send the content type and post data.

If you are using Java, it's similar. See How to use cURL in Java?

In either case, you can't send json content type directly from a form yet and will need to use server side scripting to accomplish your goal.

Hope that helps.

Community
  • 1
  • 1
zgr024
  • 1,175
  • 1
  • 12
  • 26