请问如何用jquery调用 java的接口? 本帖最后由 goolean 于 2013-11-10 10:35:05 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 试试$.post('http://IP:端口/Interface.do',{'username':'aaa','password':'23'}, $.ajax({ type: "POST", url: "http://IP:端口/Interface.do", dataType:"html", data: {'username':'aaa','password':'23'}, success: function(data){alert(data); }, error:function(){ alert($("#content" + i)+"加载失败"); } }); 没错,使用jquery的ajax,传url. <script type="text/javascript" >$(document).ready(function (){$('#test_post').click(function (){ $.ajax({ type: "POST", url: "http://IP:端口/Interface.do", dataType:"html", data: {'username':'aaa','password':'23'}, success: function(data){ alert(data); }, error:function(){ alert($("#content" + i)+"加载失败"); } }); });});</script> 一样毫无反映 有没有跨域啊。浏览器窗口直接 http://IP:端口/Interface.do 可以访问不打印错误信息error: function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus);}, 用火狐的firebug看下,ajax请求是否请求了。也有可能是服务器端出错了 版主,我查了一些资料,上面说jQuery是没法支持跨域post提交的。 jquery 的 jsonp <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>查询</title><script language="javascript" type="text/javascript" src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript" >$(document).ready(function (){$('#test_post').click(function (){ $.ajax({ async:false, url: "http://221.123.158.108/2.asp", type: "GET", dataType: 'jsonp', jsonp: 'jsoncallback', data: {username:'lsxk5',password:'lsxk2013'}, timeout: 5000, beforeSend: function(){ }, success: function (json) { if(json.actionErrors.length!=0){ alert(json.actionErrors); } genDynamicContent(qsData,type,json); }, complete: function(XMLHttpRequest, textStatus){ $.unblockUI({ fadeOut: 10 }); }, error: function(xhr){ alert("请求出错(请检查相关度网络状况.)"); }}); }); //fun});</script> </head><body ><a href="#" id="test_post">查询</a></body></html>执行后:“请求出错(请检查相关度网络状况.)”,而事实 上http://221.123.158.108/2.asp 是正常连通的 分别显示出:200,4,parsererror {"id":1,"name":"xxxx"} 你返回这样的格式试试看。 要对方server端支持跨域调用AJAX才可以的,否则不行的 肯定是可以的,对方给出的是xml格式的结果 直接执行http://221.123.158.108/2.asp接口的结果:<?xml version="1.0" encoding="UTF-8"?><result><resultcode>0</ resultcode ><taskcode>20130213231023</taskcode><errordescription>okokok</errordescription></result> 这次不用jquery,直接用ajax代码function opp(){ if(window.XMLHttpRequest) { isIE = false; xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { isIE = true; xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } try { xmlhttp.open("POST","http://221.123.158.108/2.asp",false); xmlhttp.send(null); } catch(e) { alert(e);//这里在ie下不执行,在其它浏览器显示 Error:NETWORK_ERR:XMLHttpRequest Excption 101 } var xmlDoc = xmlhttp.responseXML.documentElement; alert(xmlDoc); //这里在IE下执行显示结果为 null var x=xmlDoc.getElementsByTagName("taskcode")[0].nodeValue;; alert(x);} 等高人出现给个能实现的例子,无论哪种方法,jquery或js 如果返回的是xml 你用jquery 的jsonp 不可以,它只支持回调json 的格式 。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title> 查询 </title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> <script type="text/javascript"> jQuery(document).ready(function() { $.ajax({ type: "get", async: false, data: { "postalcode": "10504", "country": "US" }, url: "http://www.geonames.org/postalCodeLookupJSON", dataType: "jsonp", jsonp: "callback", success: function(json) { var data = json.postalcodes[0]; for (var e in data) { alert(e + "--->" + data[e]) } }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus); } }); }); </script> </head> <body> <a href="#" id="test_post"> 查询 </a> </body></html> 首先感谢rui888 一直关注本贴,不过,把URL地址换成http://221.123.158.108/2.asp ,就只执行error: function{}的部分,是怎么回事? 转换错了,上面说了。你返回的是xml,服务端改成类似json 的格式试试。 要不服务端写成个 webservice 哦,我看你的例子地址http://www.geonames.org/postalCodeLookupJSON 返回的是 json 的格式结果,那么对于xml的,有办法吗? 或者换个角度,象这类输出结果为xml的接口,应该用哪种方法去调用? 如果非要操作的话,就得生成代理 。把他的xml生成到自己这边,然后再操作。给个链接了解下吧 菜鸟求教啊!Itext PDF怎么做目录啊 并且点击目录可以跳转相应的页面去!! dwr调用的方法没执行成功的问题 一个session小问题,请大家关注下…… JSP中取.properties文件的乱码问题(急) 关于JICQ的问题 新手请教编译的问题 用logic:iterate提示No getter method for property name of bean testBeans,請幫忙 兄弟们帮忙看看,急~~~~啊!! 如何在weblogic上部署用struts结构开发的程序??? 关于报表问题! 面试时被问到项目的业务流程该怎么回答? 怎么样多外部自动填入某网站的用户名及密码
试试
$.post('http://IP:端口/Interface.do',{'username':'aaa','password':'23'}, $.ajax({
type: "POST",
url: "http://IP:端口/Interface.do",
dataType:"html",
data: {'username':'aaa','password':'23'},
success: function(data){
alert(data);
},
error:function(){
alert($("#content" + i)+"加载失败");
}
});
$(document).ready(function ()
{
$('#test_post').click(function (){
$.ajax({
type: "POST",
url: "http://IP:端口/Interface.do",
dataType:"html",
data: {'username':'aaa','password':'23'},
success: function(data){
alert(data);
},
error:function(){
alert($("#content" + i)+"加载失败");
}
});
});
});
</script>
一样毫无反映
浏览器窗口直接 http://IP:端口/Interface.do 可以访问不
打印错误信息
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
},
用火狐的firebug看下,ajax请求是否请求了。也有可能是服务器端出错了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>查询</title>
<script language="javascript" type="text/javascript" src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function ()
{
$('#test_post').click(function (){
$.ajax({
async:false,
url: "http://221.123.158.108/2.asp",
type: "GET",
dataType: 'jsonp',
jsonp: 'jsoncallback',
data: {username:'lsxk5',password:'lsxk2013'},
timeout: 5000,
beforeSend: function(){
},
success: function (json) {
if(json.actionErrors.length!=0){
alert(json.actionErrors);
}
genDynamicContent(qsData,type,json);
},
complete: function(XMLHttpRequest, textStatus){
$.unblockUI({ fadeOut: 10 });
},
error: function(xhr){
alert("请求出错(请检查相关度网络状况.)");
}
});
}); //fun
});
</script>
</head>
<body >
<a href="#" id="test_post">查询</a>
</body>
</html>
执行后:“请求出错(请检查相关度网络状况.)”,而事实 上http://221.123.158.108/2.asp 是正常连通的
分别显示出:200,4,parsererror
<?xml version="1.0" encoding="UTF-8"?>
<result>
<resultcode>0</ resultcode >
<taskcode>20130213231023</taskcode>
<errordescription>okokok</errordescription>
</result>
function opp(){
if(window.XMLHttpRequest)
{
isIE = false;
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
isIE = true;
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
try
{
xmlhttp.open("POST","http://221.123.158.108/2.asp",false);
xmlhttp.send(null);
}
catch(e)
{ alert(e);//这里在ie下不执行,在其它浏览器显示 Error:NETWORK_ERR:XMLHttpRequest Excption 101
}
var xmlDoc = xmlhttp.responseXML.documentElement;
alert(xmlDoc); //这里在IE下执行显示结果为 null
var x=xmlDoc.getElementsByTagName("taskcode")[0].nodeValue;;
alert(x);
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>
查询
</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script type="text/javascript">
jQuery(document).ready(function() {
$.ajax({
type: "get",
async: false,
data: {
"postalcode": "10504",
"country": "US"
},
url: "http://www.geonames.org/postalCodeLookupJSON",
dataType: "jsonp",
jsonp: "callback",
success: function(json) {
var data = json.postalcodes[0];
for (var e in data) {
alert(e + "--->" + data[e])
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
} }); });
</script>
</head>
<body>
<a href="#" id="test_post">
查询
</a>
</body></html>