jordaniac89
jordaniac89

Reputation: 574

Can't submit JSON object to JSP

I'm trying to send a JSON object to a JSP to parse. The JavaScript code is:

function sendData(field1, oper1, value1, field2, oper2, value2, field3, oper3, value3){
        var formData = {"field1":field1, "oper1":oper1, "value1":value1, "field2":field2, "oper2":oper2, "value2":value2, "field3":field3, "oper3":oper3, "value3":value3};
        $.post("<%=request.getRequestURL().toString()%>getInfo.jsp", {formData:formData}, function(response){alertHere(response)});
    }

function alertHere(){
        window.alert("Post Successful!")
    }

My submit button is:

<input type="submit" value="SEARCH" name="submit" class="srchbutton" onclick="sendData(document.getElementById('field1').value, document.getElementById('oper1').value>

There are several more fields passed in the JavaScript button on click, I just didn't want to post that long of a line.

When I try to post with text data in the form, my web developer console flashes the path to my JSP really quickly then disappears. It's too fast to see the error. If there's no data, the post is successful, as my alertHere function in $.post() is called correctly. I'm not sure if I'm missing something.

Upvotes: 1

Views: 1003

Answers (3)

jordaniac89
jordaniac89

Reputation: 574

Figured it out. The problem was that I wasn't passing my "response" to my success function, so:

function alertHere(){
    window.alert("Post Successful!")
}

should have been:

function alertHere(response){
    window.alert("Post Successful!")
}

It was probably posting correctly, but I wasn't getting a success because the response wasn't getting passed.

Upvotes: 0

Martin
Martin

Reputation: 215

Assuming you have a servlet on the server side which handles the data you are sending from the jsp page you could create a pseudo-class using javascript, then parses it to json and finally sends it to the server. for example:

javascript and jQuery

function SomeClass (){
  this.field1 = $("#field1").val();
  this.oper1 = $("#oper1").val();
  this.value1 = $("#value1").val();
  // etc. for every field you want to send
}

note: i'm assuming every field have an id.

function alertHere(){
   window.alert("Post Successful!")
}

jQuery and ajax

$("#someID").click(function(){
    event.preventDefault(); <-------- if you replace the submit button for a simple button,
                                      you don't need to do this.
    var formData = new  SomeClass();

    $.ajax({
      url:"ServletName", 
      data: JSON.stringify(formData), 
      dataType:"json",
    }).done(function(response){
         alertHere(response);
    });

});

html

<input type="submit" value="SEARCH" id="someID" name="submit" class="srchbutton">

Upvotes: 1

MDJ
MDJ

Reputation: 366

Try changing this line of code

$.post("<%=request.getRequestURL().toString()%>getInfo.jsp", {formData:formData}

To

$.post("<%=request.getRequestURL().toString()%>getInfo.jsp", {formData:Json.stringify(formData)}                         

Not entirely sure if this would work, Just a suggestion.

Upvotes: 0

Related Questions