0

what modifications should i do in following code in order to post multiple values from form to my web service in form of json string ?

I have tried this as-

  <script>

    function ajaxRequest(){
        var  xmlhttp = new XMLHttpRequest();
             xmlhttp.onreadystatechange = function() {
              if(xmlhttp.readyState == 4){
                 alert(xmlhttp.readyState);
                }
                };
                var namevalue=encodeURIComponent(document.getElementById("name").value);
              //  var fname=encodeURIComponent(document.getElementById("lastname"));
                var parameters="name="+namevalue
                var url="http://localhost:41191/test/resources/postservice";
            xmlhttp.open("POST",url,true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
            xmlhttp.send(parameters)

   }
</script>

Also Using-

    function ajaxRequest(){
        var  xmlhttp = new XMLHttpRequest();
             xmlhttp.onreadystatechange = function() {
              if(xmlhttp.readyState == 4){
                 alert(xmlhttp.readyState);
                }
                };

var parameters = { "name": $('#name').val(), "firstname": $('#fname').val() };
                var url = 'http://localhost:41191/test/resources/storeincompleteform';
                xmlhttp.open('POST', url, true);
                xmlhttp.setRequestHeader('Content-type', 'application/json')
                xmlhttp.send(JSON.stringify(parameters)); }
 </script>

1 Answer 1

3
var parameters = { param1: 'value1', param2: 'value2' };
var url = 'http://localhost:41191/test/resources/postservice';
xmlhttp.open('POST', url, true);
xmlhttp.setRequestHeader('Content-type', 'application/json')
xmlhttp.send(JSON.stringify(parameters));

This will send the following data in the POST request body:

{"name":"value1","name2":"value2"}

The JSON.stringify method is natively built into modern browsers but if you want to support some legacy browsers you could reference the json2.js script.

Sign up to request clarification or add additional context in comments.

5 Comments

Also, you should consider looking at YepNope.js in order to only load json2.js to browsers where it's actually needed, and save bandwidth in the case of browsers that have native support for it.
@ArpitSolanki, could you be a little more expressive when saying not working? Did you try to inspect the AJAX request in your javascript debugging tool? Was the request sent? What did the server respond? Also I am noticing that you have specified an absolute address in your url parameter but you should be aware of the same origin policy restriction that's built into browsers and which prevents you from sending cross domain AJAX request. I hope that this is not your case but make sure that you have checked this aspect as well.
The second that i posted..its takes values from a text box..i am trying to insert this values into the database but it is not working
I have an alert after the POST request .It shows a message but nothing is happening
Did you verify if the server side script is hit? What about the same origin policy?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.