本人新手,学习ajax遇到个问题XMLHttpRequest请求数据时候 status=0,responseText为空,但是偶尔又能接收到值,服务器端response status200一直正常,直接连接jsp页面又能打开。
服务器端测试jsp
<%@ page contentType="text/html; charset=GB2312" import="myfirstweb1.*" %>
<%out.println(0);%>
<% testexit tt=new testexit("lqw");
out.println(0);
%>
<%out.println(0);%>
同一目录下的jsp文件正常
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Insert title here
welcome to Eclipse for J2EE Developer Center,欢迎!
Todayis:
<%=new java.util.Date()%>
求大神伸一把手,我快被搞疯了
Insert title here /* * Returns a new XMLHttpRequest object, or false if this browser * doesn't support it */ function sndReq() { alert("biaoji1") var name=document.getElementById("name").value; var http =new XMLHttpRequest(); http.open("post", "nihao.jsp",true); http.setRequestHeader("Content-type","application/x-www-form-urlencoded"); http.send("name="+name); http.onreadystatechange = function(){ var s=http.statusText alert(s); if(http.readyState == 4&& http.status==200){ alert(http.readyState); alert(http.status); var response =http.responseText; alert(response); }}; }
网页源码
Insert title here /* * Returns a new XMLHttpRequest object, or false if this browser * doesn't support it */ function sndReq() { alert("biaoji1") var name=document.getElementById("name").value; var http =new XMLHttpRequest(); http.open("post", "nihao.jsp",true); http.setRequestHeader("Content-type","application/x-www-form-urlencoded"); http.send("name="+name); http.onreadystatechange = function(){ var s=http.statusText alert(s); if(http.readyState == 4&& http.status==200){ alert(http.readyState); alert(http.status); var response =http.responseText; alert(response); }}; }
遇到了同样的问题,我的url设置的是http://www.baidu.com 返回的status一直是0,url换成本地的一个地址后就是200了。无措。。请问你的现在有解决方案了吗
你那个是跨域了,具体自己百度吧,我也搞不定,我这个重新找了个模板粘贴复制了一下就好了,汗.......