Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 强制函数在使用jquery.get方法接收时显示结果_Javascript_Jquery_Ajax_Get - Fatal编程技术网

Javascript 强制函数在使用jquery.get方法接收时显示结果

Javascript 强制函数在使用jquery.get方法接收时显示结果,javascript,jquery,ajax,get,Javascript,Jquery,Ajax,Get,此函数使用$.get方法从asp ashx XML文件获取结果: $.get("http://www.example.com/example.ashx", { From: txtFrom, To: txtTo, Date: txtTime }, function (data) { var arrFrom = [], arrTo = [], arrPrice = [];

此函数使用$.get方法从asp ashx XML文件获取结果:

$.get("http://www.example.com/example.ashx",
                { From: txtFrom, To: txtTo, Date: txtTime },
                function (data) {
                    var arrFrom = [], arrTo = [], arrPrice = [];
                    $(data).find("F").each(function (index, item) {
                        arrFrom[index] = [];
                        arrTo[index] = [];
                        arrPrice[index] = [];
                        $(item).find("From").each(function (i, fromIndex) {
                            arrFrom[index].push($(fromIndex).text());
                        });
                        $(item).find("To").each(function (i, toIndex) {
                            arrTo[index].push($(toIndex).text());
                        });
                        $(item).find("Price").each(function (i, priceIndex) {
                            arrPrice[index].push($(priceIndex).text());
                        });
                    });
                    /**********************************************************/
                    var htmlResult = "<table style=\"background-color:red;\">";
                    for (i = 0; i < arrFrom.length; i++) {
                        htmlResult += "<tr><td>" + arrFrom[i] + "</td>" +
                            "<td>" + arrTo[i] + "</td>" +
                            "<td>" + arrPrice[i] + "</td></tr>"
                    };
                    htmlResult += "</table>";

                    $('#divSearchResults').html(htmlResult);
                    /**********************************************************/

                }, "text");
$.get(“http://www.example.com/example.ashx",
{From:txtFrom,To:txtTo,Date:txtTime},
功能(数据){
var arrFrom=[]、arrTo=[]、arrPrice=[];
$(数据)。查找(“F”)。每个(函数(索引,项){
arrFrom[index]=[];
arrTo[index]=[];
价格[指数]=[];
$(项).find(“From”).each(函数(i,fromIndex){
arrFrom[index].push($(fromIndex.text());
});
$(项)。查找(“To”)。每个(函数(i,toIndex){
arrTo[index].push($(toIndex.text());
});
$(项目)。查找(“价格”)。每个(函数(i,价格指数){
arrPrice[index].push($(priceIndex.text());
});
});
/**********************************************************/
var htmlResult=“”;
对于(i=0;i
但是ashx需要一些时间(不可预测的)来响应。 我使用了一个函数,每5秒钟调用一次,但这是负担不起的。
如何在收到响应时正确创建htmlResult?

您的示例代码是正确的,因为您定义的匿名函数会在后端响应时立即启动。如果数据为空,则可能意味着在javascript中处理数据时,数据发生了某些变化,后端返回了空数据集,或者后端超时。要检查问题是否与后端或数据处理有关,请尝试以下操作:

$.get("http://www.example.com/example.ashx",
    { From: txtFrom, To: txtTo, Date: txtTime },
    function (data) {
        console.log(data);
        //Or in case you don't have a developer console use alert:
        alert(data);
    }, "text");
您现在应该能够验证是否确实获得了数据。如果是这种情况,那么在解析数据时,数据发生了一些变化,您应该调试代码,也许是使用静态数据集。通过发送json编码的数据而不是明文,您的生活也会变得更加轻松。我不知道你将如何用你的后端做到这一点,但我相信谷歌会有所帮助。在javascript方面,您可以将最后一行更改为:

    }, "json");

然后,您将有一个整洁的json对象来处理,而不是
find()
ing things。

如果您重播这个调用,您实际上是在对后端进行另一个调用,可能会延长它应答的时间。你的代码应该像你要求的那样工作。除了在使用它之前重新声明
htmlResult
。删除最后一个
var htmlResult作为jquery文档,我试过了。之后就完成了,但运气不好。请您参考一个样本好吗?请解释一下这是怎么不起作用的。它应该按原样工作。example.ashx需要一些时间来响应,但是GET方法不会等待响应,并且htmlResult总是空的。我想一定有这样的事情:Get->receiveRespose->showHtmlResults。