【发布时间】:2017-06-24 20:52:15
【问题描述】:
我有 15k 的 JSON 产品需要用 PHP 处理,如果我 var_dump 数组我没有问题。 如果我将 JSON 数据减少到 1000-1500,则需要 1/2 秒,而由两个执行一些 jquery 操作的按钮组成的用户界面需要几秒钟。 问题很明显,当我传递所有数据(15k)时,显示 HTML 页面需要 40/50 秒,如果我单击按钮它们不起作用。
这就是我所做的: 从外部 URL 获取 JSON,(没问题)。 使用 AJAX(POST) 将 JSON 传递给 PHP,(没问题)。 在 PHP 文件中获取字符串,在数组中编码字符串并处理每个数据,在 data_processing 没问题后,我将数据回显为
<?
echo'<div class="myclass" id="myid">';
echo'<input type="hidden" value="somedatas">';
echo'<input type="text" name="'.$json[$_POST["SOMEDATA"]].'" value="somedatas">";
....
?>
有没有更好的方法来做到这一点?我还尝试将display:none 设置为主 div,但没有任何更改。
更新 我忘记了通过,php echo 被转回 ajax 请求并附加到 div
这是我在单击确认按钮后用于一一处理数据的脚本
echo'<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#close_tab_prw").click(function(){
document.getElementById("prd_prw_tab").innerHTML = ""; //reset div
$("#prd_prw").css("display","none");
$("#container_others").css("opacity","1");
});
$("#update_all_items").click(function(){
var prd_number = $("input[name=prd_max_number]").val();
for(var i = 0; i < prd_number; i++){
var string_ok = "#confirm_form_prd_";
string_ok = string_ok.concat(i);
var dataString = $(string_ok).serialize();
$.ajax( {
type: "POST",
url: "';echo $dir; echo'",
data: dataString,
success: function(data) {
console.log(data);
$("#prd_prw_tab").html(data);
}
});
}
});
$("#disable_img").click(function(){
$(".img_check").prop("checked", false);
});
$("#allow_img").click(function(){
$(".img_check").prop("checked", true);
});
$("#disable_desc").click(function(){
$(".n_desc_chk").prop("checked", false);
});
$("#allow_desc").click(function(){
$(".n_desc_chk").prop("checked", true);
});
});
</script>';
这是我用来展示产品的主要功能:
$(document).ready(function(){
$("#get_json_btn").click(function(){
document.getElementById("unique_json_return").innerHTML = ""; //reset div
if(document.getElementById('url_json').value != "") {
$.getJSON(document.getElementById('url_json').value , function(result){
$.each(result, function(i, field){
$("#unique_json_return").append(JSON.stringify(result));
})
})
.fail(function() { alert('getJSON request failed, see tips '); })
}
else {
alert("No Url insrted, please, insert your JSON url");
}
});
$("#update_products").click(function(){
new_height = "";
$( window ).resize(function() {
$("#prd_prw").css("height", $(window).height()-(20*$(document).height()/100));
});
new_height = $(window).height()-(20*$(document).height()/100);
$("#prd_prw").css("height", new_height);
$("#prd_prw").css("display","block");
$("#container_others").css("opacity","0.3");
var entire_json = document.getElementById('unique_json_return').innerText;
var one = document.getElementsByName('one')[0].value;
var two = document.getElementsByName('two')[0].value;
...
var ten = document.getElementsByName('ten')[0].value;
if(entire_json == "") {
alert("Invalid or Empty JSON response");
}
else{
$.ajax({
url: '<?echo $dir; ?>',
data: {entire_json : entire_json, one:one, two:two, three:three,...ten:ten
},
type: 'POST',
success: function(data) {
document.getElementById("unique_json_return").innerHTML = ""; //reset div
$("#prd_prw_tab").append(data);
}
});
}
});
});
【问题讨论】:
-
您不需要将 html 显式回显到页面。 this question has some good information
-
你的回声没有用。添加您正在解释的代码。我们比您的话更了解代码。这可能是 javascript 解析器问题(使用低性能 PC),也可能是 PHP 的错误编码。我们不知道。
-
这是一个非常大的代码.. @MarcosPérezGude
-
在不完全理解您的问题的情况下,考虑对结果集进行分页。见stackoverflow.com/questions/3705318/…
-
@AndreaLoda stackoverflow 的规则说你需要附上一个最小的、完整的和可验证的例子,而不是整个代码。查看更多:How to ask || MCVE
标签: javascript php jquery json ajax