javascript没有看到Spring MVC映射java
嘿,我有一个ajax请求,当我按下按钮时会调用它,但是servlet映射不起作用。当我按下按钮时,它不会进入控制器。 这是向控制器发出ajax请求的脚本
<script type="text/javascript">
function getData() {
var dataToBeSent = {
uName : $("#jsonResponse").text() //
};
$.ajax({
url : 'something/testing', // Your Servlet mapping or JSP(not suggested)
data : dataToBeSent,
type : 'POST',
dataType : 'html', // Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
success : function(response) {
$('#outputDiv').html(response); // create an empty div in your page with some id
},
error : function(request, textStatus, errorThrown) {
alert(errorThrown);
}
});
};
</script>
#json响应是一个首先动态分配的
。 这是控制器:
@Controller
@RequestMapping("/something")
public class Test {
@RequestMapping("/testing")
public void test(@RequestBody String yey){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/api?user=root&password=1234");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String query = "INSERT INTO test(Id, nume) value (default, ?)";
PreparedStatement preparedStmt;
try {
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, yey);
preparedStmt.execute();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(yey);
}
}
共 (0) 个答案