Ajax 异步传输

2019-03-25 13:32|来源: 网路

function getData(){
		var url = "Show.do?dh=1";
		createXmlHttpReq();
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
				var resp = xmlHttp.responseText;
				Convert(resp);
			}
		}
		xmlHttp.open("get",url,true);
		xmlHttp.send(null);
	}
	
	function Convert(resp){
		var mtb = document.getElementById("my_tb");
		var par = resp.parseJSON();
		var my_tb = document.getElementById("my_tb");
		for(var i = 0 ; i < par.length ; i ++){
			//alert(par[i].cate_id+"\t"+par[i].cate_name+"\t"+par[i].ori_price);
			var tbl = document.createElement("table");
			var tbody = document.createElement("tbody");
			var tr = document.createElement("tr");
			var td = document.createElement("td");
			td.setAttribute("vAlign","top");
			td.setAttribute("width","90");
			td.setAttribute("height","80");
			td.innerHTML = "<A href="+par[i].img_path+" target=_blank><IMG height=80 alt=点击图片查看内容 src="+par[i].img_path+" width=80 border=0></A>";
			var td2 = document.createElement("td");
			td2.setAttribute("vAlign","top");
			var tb = document.createElement("table");
			tb.setAttribute("cellSpacing","1");
			tb.setAttribute("cellPadding","0");
			tb.setAttribute("width","100%");
			tb.setAttribute("align","center");
			tb.setAttribute("border","0");
			var body = document.createElement("tbody");
			var tr2 = document.createElement("tr");
			var td3 = document.createElement("td");
			td3.innerHTML = "<A href=# target=_blank><STRONG>"+par[i].cate_name+"</STRONG></A>";
			var tr3 = document.createElement("tr");
			var td4 = document.createElement("td");
			td4.setAttribute("height","21");
			td4.innerHTML = "<FONT color=#ff0000>现价:人民币"+par[i].cur_price+"元</FONT><BR><a href=#>"+par[i].descript+"</a>!";
			
			
			tr2.appendChild(td3);
			tr3.appendChild(td4);
			body.appendChild(tr2);
			body.appendChild(tr3);
			tb.appendChild(body);
			td2.appendChild(tb);
			tr.appendChild(td);
			tr.appendChild(td2);
			tbody.appendChild(tr);
			tbl.appendChild(tbody);
			mtb.appendChild(tbl);
	    }
	    alert("完成");
	}



<TABLE cellSpacing=0 cellPadding=0 width=776 align=center border=0 id = "my_tb">
	
</TABLE>


这代码有问题吗,为什么运行至 alert("完成") 消息弹出,  网页仍然没有任何字迹出现,求老哥们帮忙看看。

问题补充:
dieslrae 写道
var par = resp.parseJSON();


parseJSON如果我没记错这是火狐才有方法,老老实实的用eval吧

var par = eval("(" + resp + ")")//如果resp是数组就不用+()了



我用了js模板的。
问题已经解决了,谢谢。

相关问答

更多
  • 就上面的描述,我觉得你的Ajax没有成功执行。你在你的ajax 函数中添加 error:function(e){
    alert(e);
    }
    看会不会弹出
  • 你好!! ①使用jQuery的load函数,轻松搞定!! $(function(){ $("div").load("你的url",{"参数名" : "参数值"},function(){ $(this).html( $(this).find("td[width='501']").html() ); }); }); ②修改数据的这个问题,不太理解。 提交表单--->写入数据库?--->修改数据? ajax技术主要用于异步的数据传输,就技术本身而言,是可以做到这点的。
  • jquery可以实现前端和后端之间的交互,默认的就是异步的。但是可以设置async:false来改成同步的。 实际运用的方法如下: $.ajax( url : '请求接口的路径', type :'post或者get', dataType : '数据类型,一般是json,也支持jsonp(跨域访问时使用)', success : function(e){ //成功后调用的函数 //e.data为返回的数据 }, error : function(){ //发生异常后调用的函数 } )
  • 你可以使用这样的东西.. // Variables from MySQL request: $event $date $place ///pass all the parameters in onclick function as arguments
  • 将您的功能更改为: render: function (forArr) { saved_selection = JSON.parse(window.localStorage.getItem('match_selection')); html_string = template({forArr:forArr, saved_selection:saved_selection}); console.log(saved_selection); if(UsefulFuncs.ge ...
  • 好,谢谢。 经过几个小时的分析和反思,我意识到为什么这个脚本会同步:我打开script.php文件,我注意到这个和文件的beginig: 所以我有一个使用会话的php脚本并行ajax调用,但会话在这种情况下锁定了要执行syncrosnly会话变量请求的原因,所以这个问题的解决方案是:
  • async:true是jQuery框架中的默认值 如果你想通过$.ajaxSetup()函数进行管理,因为$.get dose不提供传递附加设置参数, $.ajax()会更好,更直观 要么 $.ajaxSetup({async:true}); 全球设置 async:true is default in jQuery framework If you want manage by $.ajaxSetup() function, because $.get dose not provide the pass a ...
  • 来自jQuery网站 : 默认情况下,所有请求都是异步发送的(默认情况下设置为true)。 如果需要同步请求,请将此选项设置为false。 跨域请求和dataType:“jsonp”请求不支持同步操作。 请注意,同步请求可能会暂时锁定浏览器,在请求处于活动状态时禁用任何操作。 这里的所有都是它的。 如果您需要有关特定问题的帮助,请告诉我。 From the jQuery site: By default, all requests are sent asynchronously (i.e. this is ...
  • 来自javascript的异步请求与服务器上的异步任务不同。 来自javascript的异步任务仍然在服务器上同步处理,因此,您可能会遇到大型应用程序上的线程池饥饿。 拥有在服务器上异步处理的异步请求是不同的,它释放了IIS线程以立即处理其他请求,而该请求来自javascript或完整的post / get在后台处理。 一些阅读可能有所帮助 http://msdn.microsoft.com/en-us/library/ee728598(v=vs.98).aspx#processing_asynchrono ...