-1

I am sending JSOn via AJAX and its null in servlet

JAVASCRIPT

Function creating JSON

  function submitTheValues(event, id, price, count) {
    var searchEleWithinDiv = document.getElementById("content").children;
    var table = searchEleWithinDiv[1];
    var qty = table.rows[count].cells[8].children[0].value;
    var acNo = table.rows[count].cells[10].children[0].value;
   var jsonStr = '{"reagentid": id, "account": acNo,"quantity":           
     qty, "reagentcount":count}';      
    var jsonObj = eval("(" + jsonStr + ")");
    return jsonObj;
  }

AJAX code var xmlhttp = new XMLHttpRequest(); ;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            if(xmlhttp.responseText !=null) 
            {
                var searchEleWithinDiv = document.getElementById("content").children;
                var table = searchEleWithinDiv[1];
                var btn = table.rows[count].cells[11].children[0].value;
                btn.value = "Added to Cart";
            }
        }
    }
    var url = "<%=request.getContextPath()%>/displaycartservlet";
    var jsonObj = this.submitTheValues(event, id, price, count);
    var jsonOb = JSON.stringify(jsonObj);


     xmlhttp.open("POST", url, true);
     xmlhttp.setRequestHeader("Content-type", "application/json");
     xmlhttp.send(jsonOb);       
}

If I change the last two statements to the following also, I get the null error

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencode");                    
xmlhttp.send('json='+encodeURIComponent(jsonOb));

SERVLET CODE

     String jsonPar = request.getParameter("json");
Raghu
  • 1,141
  • 5
  • 20
  • 39

2 Answers2

1

You're not going to be able to read JSON from a POST body with getParameter, see get POST data

To get json in the parameter I think you just forgot the d on the end of the mime type. You had "application/x-www-form-urlencode":

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send('json='+encodeURIComponent(jsonOb));

Also, you could clean up some of the code with eval which is not really needed. Just create the object directly:

function submitTheValues(event, id, price, count) {
    var searchEleWithinDiv = document.getElementById("content").children;
    var table = searchEleWithinDiv[1];
    var qty = table.rows[count].cells[8].children[0].value;
    var acNo = table.rows[count].cells[10].children[0].value;
    return {
        reagentid: id,
        account: acNo,
        quanitity: qty,
        reagentcount: count
    };
}

and then cleaned up the names here:

var url = "<%=request.getContextPath()%>/displaycartservlet";
var obj = this.submitTheValues(event, id, price, count);
var jsonObj = JSON.stringify(obj);
Community
  • 1
  • 1
Austin Greco
  • 32,997
  • 6
  • 55
  • 59
0

check the XMLHttpRequest send method, you'll see that you need the boundary parameter if you want to use the x-www-form-urlencode. But to send a simple JSON string, you could use this:

    xmlhttp.setRequestHeader("Content-type", "application/json");    

and should work (it does in my modules).

If you still want to use forms, the SUPER EASY way is this:

<form id="myform">
    <input name="text" type="text"/>
    <select name="month">
        <option name="ene">Enero</option>
        <option name="feb">Febrero</option>
    </select>
</form>
<script>
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function(){
        if(this.readyState == 4 && this.status == 200)
                alert(this.responseText);
    }

    xhr.open("POST", "server.php");//i use php, but you can change to your servlet here

    function send(){
        xhr.send(new FormData(document.getElementById('myform')));
    }

</script>

<button onclick="send()">Enviar</button>  

Where the key is using the FormData Object

eleuteron
  • 1,863
  • 20
  • 26