[英]ajax jquery - post csv file to php one line at a time
我想做這個:
問題是:
我認為結合 jquery:
我堅持使用 jquery,因為它以無序方式發送 csv 中的數據。
我需要幫助的是暫停 jquery.post 在示例中每個 1 秒間隔。
這是我到目前為止提出的代碼。 我需要每個帖子的“暫停”效果
<script>
$(function () {
var o1;
var o2;
$.ajaxSetup({cache: false});
$('#load').click(function () {
$.get($('#file').val(), function (content) {
//get the file and convert into single array
o1 = content.split(new RegExp("\n")).map(function (element) {return $.trim(element).toLowerCase();})
//---------------------------------------------------------------------------- Display
$("#box").empty();
$.each(o1,
function( i, j ){
if (j!=""){
//add the tr
//$("#tbl").append($( "<tr id=\"tr-"+i+"\"></tr>" ));
//split again
o2 = j.split(new RegExp(",|\r")).map(function (element) {return $.trim(element).toLowerCase();});
$.post('test2.php',{jquery:'1' , d:o2[0] , o:o2[1] , h:o2[2] , l:o2[3] , c:o2[4]},function(res){
$("#box").append(res);
});
}//end if
}//end function
);//end each
//---------------------------------------------------------------------------- end of display
});
});
});
</script>
</head>
<body>
<form>
<input id="file" type="file" />
<input type="button" id="load" value="Load CSV" />
</form>
<hr />
<table id="tbl" border="1"><tbody></tbody></table>
<div id="box"></div>
<ol id="list"></ol>
</body>
這是“test2.php”文件內容。 這只是我將要做的一個例子,真正的代碼當然更復雜:
if ($_POST){
@$_POST['jquery']!=''?$jquery=$_POST['jquery']:$jquery='';
if ($jquery!=''){
@$_POST['d']!=''?$d=$_POST['d']:$d='';
@$_POST['o']!=''?$o=$_POST['o']:$o='';
@$_POST['h']!=''?$h=$_POST['h']:$h='';
@$_POST['l']!=''?$l=$_POST['l']:$l='';
@$_POST['c']!=''?$c=$_POST['c']:$c='';
echo 'D: '.$d.' - '.$o.' - '.$h.' - '.$l.' - '.$c."<br />\n";
}
}
這里真的需要幫助。 謝謝。
即使我不是您解決問題的方式的堅定支持者...
停止 jQuery 進行異步調用: async: false
您可以在 jQuery 中執行此操作: $.ajaxSetup({async:false});
然后定期撥打$.post
電話。 有關手冊的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.