Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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";
    }
}
share|improve this question
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: 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. –  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
show 1 more comment

2 Answers 2

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.

share|improve this answer
    
Ok Bergi Thanks for your answer i will check it. –  ConceptSpecs Jun 6 '12 at 7:18
1  
Don't say "thanks", just upvote the answer when it's worth. Then, after checking, you can accept it when it's correct. –  Bergi Jun 6 '12 at 7:35
add comment

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.

share|improve this answer
    
Thanks Ryan I think u r right i am checking it. –  ConceptSpecs Jun 6 '12 at 7:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.