【发布时间】:2017-08-01 15:53:53
【问题描述】:
我想获取 'http://localhost:8080/api/printer?exclude=temperature' [HTTP/1.1] 提供的 Json 数据。
这是要提供的Json数据。
{
"state": {
"text": "Operational",
"flags": {
"operational": true,
"paused": false,
"printing": false,
"sdReady": true,
"error": false,
"ready": true,
"closedOrError": false
}
}
}
类型是'GET',Content-Type 是'application/json'
以下是我的 JSP 完整源代码。
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" language="javascript">
function print_stat(){
jQuery.ajax({
type: 'GET',
async: false ,
url: 'http://localhost:8080/api/printer?exclude=temperature&apikey=1234567...',
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert("TEST");
},
error : function(x, e) {
alert('server error occoured');
if(x.status==0){ alert('0 error');
}else if(x.status==404){ alert('404 error');
}else if(x.status==500){ alert('500 error');
}else if(e=='parsererror'){ alert('Error.nParsing JSON Request failed.');
}else if(e=='timeout'){ alert('Time out.');
}else { alert(x.responseText); }
}
});
}
</script>
<body>
<input type="button" onclick="print_stat()" value="test">
</body>
我看不到“测试”弹出窗口。 什么都没有发生。
我应该如何获取数据?
请帮帮我。
【问题讨论】:
-
async: false , -
所以开发者控制台没有报错?
-
你是否包含了 jquery?听起来不像。
-
您的代码适用于我自己的网址。你的网址中的那些点是什么?
-
该按钮是否在导致页面重新加载的表单中?
标签: javascript jquery ajax rest jsp