0

In my JSP (index.jsp), I am submitting a form that passes the Parameters to another JSP (action.jsp) using JSON.

<form id="form">
 <input type="text" name="name"/>
 <input type="button" id="btn" value="OK"/> 
</form>

And here is the Jquery Code of index.jsp :

$(document).ready(function(){
 $("#btn").click(function(){
   var name = $("input[name=name]").val();
   $.ajax({
                    cache:false,
                    url:"action.jsp",
                    data:{name:name},
                    contentType:"application/json; charset=utf-8",
                    dataType:"json",
                    success:function (msg) {
                       alert("SUCCESS");
                       // here I want a String that is 
                       // generated(generatedString) in action.jsp.
                        });
                       }
                    },
                    error:function () {
                        alert("Unable to add.");
                    }
        }); 

 });
});

My action.jsp code :

<%
   String name = request.getParameter("name");
   String generatedString = "E5142"; 
   response.setContentType("application/json");
   out.print(generatedString);
%>

The form is submitted to action.jsp successfully but cannot get the value 'generatedString' in index.jsp in return.

UPDATED: index.jsp always showing error in alert : "Unable to add." Tried what is said in comments, but didn't come up with success message.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sunny
  • 73
  • 1
  • 1
  • 9
  • I dont know JSP but it looks like youre just defining your variables not printing them? You need to print out the string in json. – mindore Apr 08 '15 at 20:18
  • In action.jsp, try adding another line `out.print(generatedString)`. Then inside of your `success` callback replace `alert("SUCCESS")` with `alert(msg)` – Michael Hommé Apr 08 '15 at 21:58
  • See this [similar question](http://stackoverflow.com/questions/26279339/ajax-post-cannot-send-data-to-servelet/26280336#26280336) – Tap Apr 09 '15 at 00:05
  • tried but didn't work; – Sunny Apr 09 '15 at 05:59
  • maybe the url is wrong, that is why you get the error message. Make user that action.jsp is in the same level(folder) with index.jsp – MaVRoSCy Apr 09 '15 at 06:10
  • and if the url is correct, then have a look at http://stackoverflow.com/questions/4902008/retrieve-the-data-sent-as-json-from-javascript-inside-a-servlet on how to retrieve json values from javascript in jsp – MaVRoSCy Apr 09 '15 at 06:14
  • that is not an issue dear. I have set the JSP in debug mode, Like what I said , it successfully passes the control to action.jsp, but always showing error in alert .@MaVRoSCy – Sunny Apr 09 '15 at 06:14

1 Answers1

0

If you want to send response from a jsp page you can use out.println().

<%
   String jsonString= request.getParameter("name");
   String generatedString = "E5142";
   out.println(generatedString);
%>

In your jquery you can use alert(msg);

But your problem is about action.jsp url path or jsp file. Because you are getting error in jquery send action! In google chrome open your console (press F12). Send your action and you can see error on console. If you get "404 error" this is a path error. If you get "500 error" this a jsp file error. Control your system log for what is going on.

Can you try this:

var name = $("input[name=name]").val();
var url = "action.jsp";
$.post(url,{name:name})
    .done(function(data){
        alert(data);
    })
    .fail(function(){
        console.log('fail');
    });
hurricane
  • 6,521
  • 2
  • 34
  • 44