为何我的ajax用http://localhost/本地测试可以,用url:"http://10.63.27.117/admin/129.asp",就不行?
function get_txm(){
// var name = $('input[name="username"]').val();
// alert(name);
// document.f1.submit();
var txmid = document.getElementById("txm").value;
$.ajax({
url:"http://10.63.27.117/admin/129.asp",
type: "POST",
data: {
id: txmid,
},
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success:function(res){
if (res) {
var arr = res.split('|');
//$("#div2").html(arr[0]);//在DIV里显示
//打开设备所属分类上色
var tDiv = document.getElementById('u'+arr[0]);
tDiv.style.backgroundColor = '#FF0066';
//打开设备所属div
var tg=document.getElementById('l'+arr[0]);
tg.style.display="";
//选中该ID复选框
var chk=document.getElementById('k'+arr[1]);
chk.checked = true;
}
}
})
}
可能因为跨域了
【相关推荐】
处理的servlet,别名为aj
package cn.liu.ajax;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Ajax extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//请求数据编码格式
req.setCharacterEncoding("utf-8");
//响应编码数据格式
resp.setContentType("text/html; charset=utf-8");
resp.getWriter().write("这是ajax");
}
}
处理的jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>ajax练习</title>
<meta content="text/html; charset=utf-8">
<!-- 声明js代码域 -->
<script type="text/javascript">
//声明getDate函数,并不是调用,调用在body中的input中
function getData(){
//1.创建ajax引擎对象
var ajax;
if(window.XMLHttpRequest){
ajax=new XMLHttpRequest();
}else{
ajax=new ActiveXObject("Msxml2.XMLHTTP");
}
//3.覆写
ajax.onreadystatechange=function(){
if(ajax.readyState==4){//此时才有响应数据
if(ajax.status==200){
//按文本拿到响应内容
var content = ajax.responseText;
//获取元素对象,在此div种展示内容
var showdiv=document.getElementById("showdiv");
showdiv.innerHTML=content;
}else if(ajax.status==404){
//获取元素对象
var showdiv=document.getElementById("showdiv");
showdiv.innerHTML="请求资源不存在";
}else if(ajax.status==500){
//获取元素对象
var showdiv=document.getElementById("showdiv");
showdiv.innerHTML="服务器繁忙";
}else{
//获取元素对象
var showdiv=document.getElementById("showdiv");
showdiv.innerHTML="请稍后再访问";
}
}
}
//2.使用ajax引擎对象创建请求
ajax.open("get", "aj");
//4.使用ajax对象发送请求
ajax.send(null);
}
</script>
<style type="text/css">
#showdiv{
border:solid 1px;
width:200px;
height:100px;
}
</style>
</head>
<body>
<h3>展示页面</h3>
<hr>
<input type="button" value="start" onclick="getData()"/>
<div id="showdiv"></div>
</body>
</html>
展示效果:
点击start后div中刷新,当前页面不刷新。