在前端网页我想用jquery Ajax访问另外一个域的java controller post method后台服务,比如本地页面是http://localhost/test/test.html添加js代码:
<script type="text/javascript">
$(document).ready(function () {
$("#btnTest").click(function() {
$.ajax({
//contentType:"application/json;charset=UTF-8",
type: "POST",
cache: false,
//url:"http://localhost/test/send.do",这个地址是可以得到正确的msg.responseText值
//url: "http://localhost/app-admin/send.do?callback=?",这个地址是可以得到正确的msg.responseText值
url: "http://192.168.0.100/test/send.do",//这个是远程部署java后台方法的地址,取得msg.responseText值为空。
data: "[email protected]&password=111111",
dataType: "jsonp",
jsonp: 'callback',
complete:function(msg)
{
alert(msg.responseText);
},
success: function(msg) {
alert(msg.responseText);
}
});
});
});
</script>
java controller post method部署在另外一台机器上面,比如192.168.0.100
post method 的代码为: @RequestMapping(value="/send.do",method=RequestMethod.POST)
public void sendEamil(HttpServletRequest request,HttpServletResponse response) throws IOException
{
String email = request.getParameter( "email" );
String password = request.getParameter( "password" );
String callback = request.getParameter( "callback" );
String resultMsg = null;
PrintWriter out = null;
if(email== null || email.equals( "" ) ||
password == null || password.equals( "" )){
resultMsg = callback + "({\"status\":\"0\"})";
}else{
boolean isSignin = true;
if(isSignin){
resultMsg = callback + "({\"status\":\"1\"})";
}else{
resultMsg = callback + "({\"status\":\"0\"})";
}
}
try {
out = response.getWriter();
} catch (IOException e) {
}
out.write(resultMsg);
out.close(); }
现在分为几种情况:
1、如果java后台代码和前台部署在同一台机器上,并且把ajax的url设置为http://localhost/test/send.do,那么在complete方法里面可以正常取得msg.responseText值
2、如果java后台代码和前台部署在不同机器上,并且把ajax的url设置为远端机器ip比如:http://192.168.0.100/test/send.do,那么在complete方法里面可以取得msg.responseText为空
请问有遇到过这种情况的朋友吗?
<script type="text/javascript">
$(document).ready(function () {
$("#btnTest").click(function() {
$.ajax({
//contentType:"application/json;charset=UTF-8",
type: "POST",
cache: false,
//url:"http://localhost/test/send.do",这个地址是可以得到正确的msg.responseText值
//url: "http://localhost/app-admin/send.do?callback=?",这个地址是可以得到正确的msg.responseText值
url: "http://192.168.0.100/test/send.do",//这个是远程部署java后台方法的地址,取得msg.responseText值为空。
data: "[email protected]&password=111111",
dataType: "jsonp",
jsonp: 'callback',
complete:function(msg)
{
alert(msg.responseText);
},
success: function(msg) {
alert(msg.responseText);
}
});
});
});
</script>
java controller post method部署在另外一台机器上面,比如192.168.0.100
post method 的代码为: @RequestMapping(value="/send.do",method=RequestMethod.POST)
public void sendEamil(HttpServletRequest request,HttpServletResponse response) throws IOException
{
String email = request.getParameter( "email" );
String password = request.getParameter( "password" );
String callback = request.getParameter( "callback" );
String resultMsg = null;
PrintWriter out = null;
if(email== null || email.equals( "" ) ||
password == null || password.equals( "" )){
resultMsg = callback + "({\"status\":\"0\"})";
}else{
boolean isSignin = true;
if(isSignin){
resultMsg = callback + "({\"status\":\"1\"})";
}else{
resultMsg = callback + "({\"status\":\"0\"})";
}
}
try {
out = response.getWriter();
} catch (IOException e) {
}
out.write(resultMsg);
out.close(); }
现在分为几种情况:
1、如果java后台代码和前台部署在同一台机器上,并且把ajax的url设置为http://localhost/test/send.do,那么在complete方法里面可以正常取得msg.responseText值
2、如果java后台代码和前台部署在不同机器上,并且把ajax的url设置为远端机器ip比如:http://192.168.0.100/test/send.do,那么在complete方法里面可以取得msg.responseText为空
请问有遇到过这种情况的朋友吗?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货