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";
    }
}
6
  • 7
    What the hell. You have jQuery but you create XHRs manually and use native DOM methods - that makes no sense. Commented May 28, 2012 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. Commented May 28, 2012 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 Commented May 28, 2012 at 11:51
  • As ThiefMaster said, please read these: api.jquery.com/jQuery and 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. Commented May 28, 2012 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() Commented May 29, 2012 at 13:42

2 Answers 2

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.

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

Comments

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.

Comments

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.