$.ajax({
url:"http://192.168.1.202:8080/bracelet",
data:{'id':$('#show_hand').val()},
type:"post",
success:function(result){
json = eval(result);
alert(json.DeviceCode);
alert(json.InDBTime);
//进入 enter、待命standby、离开exit
alert(json.Status);
_interval = setInterval("ajaxSaat()", 5000);
}
});
}
function saatStop(){
$.ajax({
url:"http://192.168.1.202:8080/bracelet",
data:{'id':$('#show_hand').val()},
type:"delete",
success:function(result){
json = eval(result);
alert(json.DeviceCode);
alert(json.InDBTime);
//进入 enter、待命standby、离开exit
alert(json.Status);
}
});
}
犹豫c++那边是通过请求类型进行区别操作的,所以jsonp跨域直接放弃
然后百度了半天,发现可以通过java做一下代理的功能
ps:跨域问题:跨域是我在日常面试中经常会问到的问题,这词在前端界出现的频率不低,主要原因还是由于安全限制(同源策略, 即JavaScript或Cookie只能访问同域下的内容),因为我们在日常的项目开发时会不可避免的需要进行跨域操作,所以跨域能力也算是前端工程师的基本功之一。
所以java是不存在跨域问题的:
js改为如下:
function saatStart(){
var show_hand = $('#show_hand').val();
$.ajax({
url:"<%=path%>/regOne/saatStart.do",
data:{'id':show_hand},
type:"post",
success:function(result){
json = eval(result);
alert(json.data);
_interval = setInterval("ajaxSaat()", 5000);
}
});
}
function saatStop(){
var show_hand = $('#show_hand').val();
$.ajax({
url:"<%=path%>/regOne/saatStop.do",
data:{id:show_hand},
type:"post",
success:function(result){
json = eval(result);
alert(json.data);
}
});
}
java端代码:
@RequestMapping("/saatStart")
public void saatStart(String id,HttpServletRequest req, HttpServletResponse resp) throws MalformedURLException, IOException {
String url = "http://192.168.1.202:8080/bracelet";
// 取得连接
HttpURLConnection huc = (HttpURLConnection) new URL(url).openConnection();
// 设置连接属性
huc.setDoOutput(true);
huc.setRequestMethod("POST");
huc.setUseCaches(false);
huc.setInstanceFollowRedirects(true);
huc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
huc.connect();
// 往目标servlet中提供参数
OutputStream targetOS = huc.getOutputStream();
targetOS.write(("id="+id).getBytes());
targetOS.flush();
targetOS.close();
// 取得页面输出,并设置页面编码及缓存设置
resp.setContentType(huc.getContentType());
resp.setHeader("Cache-Control", huc.getHeaderField("Cache-Control"));
resp.setHeader("Pragma", huc.getHeaderField("Pragma"));
resp.setHeader("Expires", huc.getHeaderField("Expires"));
OutputStream os = resp.getOutputStream();
// 将目标servlet的输入流直接往页面输出
InputStream targetIS = huc.getInputStream();
int r;
while ((r = targetIS.read()) != -1) {
os.write(r);
}
targetIS.close();
os.flush();
os.close();
huc.disconnect();
}
@RequestMapping("/saatStop")
public void saatStop(String id,HttpServletRequest req, HttpServletResponse resp) throws MalformedURLException, IOException {
String url = "http://192.168.1.202:8080/bracelet?id="+id;
// 取得连接
HttpURLConnection huc = (HttpURLConnection) new URL(url).openConnection();
// 设置连接属性
huc.setDoOutput(true);
huc.setRequestMethod("DELETE");
huc.setUseCaches(false);
huc.setInstanceFollowRedirects(true);
huc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
huc.connect();
// 取得页面输出,并设置页面编码及缓存设置
resp.setContentType(huc.getContentType());
resp.setHeader("Cache-Control", huc.getHeaderField("Cache-Control"));
resp.setHeader("Pragma", huc.getHeaderField("Pragma"));
resp.setHeader("Expires", huc.getHeaderField("Expires"));
OutputStream os = resp.getOutputStream();
// 将目标servlet的输入流直接往页面输出
InputStream targetIS = huc.getInputStream();
int r;
while ((r = targetIS.read()) != -1) {
os.write(r);
}
targetIS.close();
os.flush();
os.close();
huc.disconnect();
}
此处java发送delete请求无法写入参数,只能通过url?id=id这种方式