0

I want to send JSON converted data (html code) to the requestGet Servlet. My code is absolutely correct in which POST request is sent to the servlet but I have an error in conversion of string to JSON.

I am using myeclipse in which when I run this code it shows

"JSON is undefined"

but when I save it as HTML and run on FF it neither shows any error nor sends any request to the servlet. Please suggest whether my method is correct for sending JSON text to servlet by POST method.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.json.org/json2.js"></script>
<SCRIPT language="javascript">

var counter=0;
var controls=new Array();
function add(type) {
    //Create an input type dynamically.
    var element = document.createElement("input");

    //Assign different attributes to the element.
    element.setAttribute("type", type);
    element.setAttribute("value", type);
    element.setAttribute("name", type);
    element.id=type+counter;
    controls[counter]=element.id;
    counter++;
    var foo = document.getElementById("fooBar");
    foo.appendChild(element);
}

function save(){
    var data="";
    var formTitle="Form1";
    var method="post";
    data="<HTML><HEAD><TITLE>"+formTitle+"</TITLE></HEAD><BODY><FORM METHOD="+method+"/>";
    for(i=0;i<controls.length;i++){
    var element=document.getElementById(controls[i]);
    data+="<INPUT type=button id="+element.id+" value="+element.getAttribute("value")+"/>";
    }
    data+="</FORM></BODY></HTML>";
    alert("Data::"+data);
    DoSelectRecommendation(data);

} 

/*
 * code for sending request to the servlet.
 */
$(function() {
       var frm = $(document.myform);
       var dat = JSON.stringify(frm.serializeArray());
       alert("I am about to POST this:\n\n" + dat);

       $.post(
         frm.attr("action"),
         dat,
         function(data) {
           alert("Response: " + data);
         }
       );
     });
var req;
function DoSelectRecommendation(Text) {
if(window.XMLHttpRequest && !(window.ActiveXObject)) {  
        try {  
            req = new XMLHttpRequest();  
        } catch(e) {  
            req = false;  
        }  

    } else if(window.ActiveXObject) {  

        try {  
            req = new ActiveXObject("Msxml2.XMLHTTP");  
        } catch(e) {  
            try {  
                req = new ActiveXObject("Microsoft.XMLHTTP");  
            } catch(e) {  
                req = false;  
            }  
        }  
    } 

 var url="http://localhost:8080/TestForJsp/requestGet";
 req.open("POST",url,true);
 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 req.send(Text);
 req.onreadystatechange = inserter;
}

function inserter() {
if (req.readyState == 4) {
    if (req.status == 200) {
        var msg = req.responseText;
        alert("msg = "+msg);
        if (msg == "") {
            document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+"COS NIE TAK"+"</div>";
            //document.getElementById("msg1").value = "blabla";
        }
        else
            document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+msg+"</div>";
            //document.getElementById("msg1").value = "COOOO JEST";
    }
}
Widor
  • 13,003
  • 7
  • 42
  • 64
ConceptSpecs
  • 71
  • 2
  • 9
  • 7
    What the hell. You have jQuery but you create XHRs manually and use native DOM methods - that makes no sense. – ThiefMaster May 28 '12 at 10:43
  • 2
    +1 to previous comment. As well as why you stringify serialized form data for using it in your `post` request. That also doesn't make any sense. – VisioN May 28 '12 at 10:46
  • actually i got this code from net please suggest me how to send data in json format from my form to servlet. I am trying to convert string to json but no result is there now i am using this code:- var str = '{"Data":'+cdlText+'}'; var jText=JSON.stringify(eval('(' + str + ')')); alert("CDL::"+jText); but it also doesn't works – ConceptSpecs May 28 '12 at 11:51
  • As ThiefMaster said, please read these: http://api.jquery.com/jQuery/ and http://api.jquery.com/jQuery.ajax/ . Then try not to use methods like document.createElement, document.findElement* and req = new XMLHttpRequest() . Please note the amount of code you need to make an xhr-request, and the amount you would need using jquery. After you did this, reformulate your question. – Herbert May 28 '12 at 12:12
  • 1
    @HerbertKruitbosch IMHO it's perfectly OK to mix some DOM methods with jQuery - i.e. use `this.value` instead of `$(this).val()` – Alnitak May 29 '12 at 13:42
  • @Alnitak I agree, still my point stays for ajax-requests and querying elements ;) – Herbert May 29 '12 at 23:27

2 Answers2

0

It seems to be a bug in myeclipse which does not know the The JSON Object.

As you said, your code is correct and runs perfectly in Chrome and FF, which implement the linked standard. To work around the problem, you could use parseJSON from your jQuery lib, with stringify you will need a workaround as jQuery misses a stringifyJSON method.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Not all browsers have native JSON support. Some versions of IE for example, and probably myeclipse. Consider using a shim such as Douglas Crockford's JSON2 to add support in non-compliant browsers.

Ryan Lynch
  • 7,676
  • 1
  • 24
  • 33