Issue
I am using jQuery in Spring MVC for validation purposes. Everything is OK related to validation, but the problem is that I cannot return any response from the target page to the page the request is sent. Here is the partial code of the view page ( from where the data is sent ):
$('#loginbtn').click( function() {
var haserror = false;
if( $('#username').val() == null || $.trim( $('#username').val() ) == '' ) {
$('.username').removeClass('hide');
haserror = true;
} else {
$('.username').addClass('hide');
}
if( $('#password').val() == null || $.trim( $('#password').val() ) == '' ) {
$('.password').removeClass('hide');
haserror = true;
} else {
$('.password').addClass('hide');
}
if( haserror == true ) {
return;
} else {
//console.log('OK');
$('#checklogin').load("insert", {'username':$('#username').val(), 'password':$('#password').val() }, function(d) {
//alert(d);
});
}
});
The above code is correctly validated data and sends the data to my target: insert
Here is the code written for insert:
@RequestMapping(value="/insert", method = RequestMethod.POST)
public String insert(Model model, @RequestParam(value="username", required=true) String username, @RequestParam(value="password", required=true) String password) throws SQLException {
try {
String query="INSERT INTO userlog(username, pword) VALUES('"+ username +"','"+ password +"')";
Database.insert(query);
return '1';
} catch( SQLException E) {
//E.printStackTrace();
return '2';
}
}
Code for Database.insert(query):
package com.tng.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
public static void insert( String query ) throws java.sql.SQLException {
Statement stmt = null;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/springtest","root","");
stmt= conn.createStatement();
stmt.execute(query);
try {
if(stmt != null) stmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
I am trying to return '1' if data is successfully inserted and '2' if something goes wrong ( as usual I am doing in PHP project ). But it tries to redirect me to a page with the following url: base_url/1 and base_url/2, instead of sending back '1' and '2' as a response to my source page.
Can anyone tell me how I can fix the issue & send a response from the target page.
N.B: I am new in Spring MVC, above form & data insertion are just for testing, so no encryption of password is used.
Thanks
Solution
You need to add ResponseBody annotation in your post method.
org.springframework.web.bind.annotation.ResponseBody
@RequestMapping(value="/insert", method = RequestMethod.POST)
@ResponseBody
public String insert(Model model, @RequestParam(value="username", required=true) String username, @RequestParam(value="password", required=true) String password) throws SQLException {
try {
String query="INSERT INTO userlog(username, pword) VALUES('"+ username +"','"+ password +"')";
Database.insert(query);
return "1";
} catch( SQLException E) {
//E.printStackTrace();
return "2";
}
}
Answered By - Rizwan M.Tuman