Ajax循环时出错

Ajax Error when looping

本文关键字:出错 循环 Ajax      更新时间:2023-09-26

我使用php和javascript/jquery的混合物抓取网站的价格从那里的网页,没有API,所以不幸的是我抓取html页面,并从html中获取价格/标题(我知道这是一个真正有风险的方式,但这是唯一的方法)。

总之,事情是这样的:

我使用php file_get_contents()拉入外部网页

这个方法是在foreach循环中,对于我想从每个外部页面获取数据。

这是我的javascript。

<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js' type='text/javascript'></script>
            <script type='text/javascript'>
            console.log('page-{$i}');

                $('.page-{$i}').find('.search_result_row').each(function(i, obj) {
                    //This will give us each individual apps details. 
                    var appTitle    = $(this).find('.title').text();
                    var appPrice    = $(this).find('.search_price').text();
                    var appDiscount = $(this).find('.search_discount').text();
                    var appDetails = {
                        'appTitle'    : appTitle,
                        'appPrice'    : appPrice,
                    };
                    callAjax(appDetails);
                    var my_delay = 5000;
                    function callAjax(appDetails) {
                        $.ajax({
                         url: 'Upload.php',
                          type: 'POST',
                          data: appDetails,
                          dataType: 'JSON',
                         success:function(data) {
                                console.log(data);
                           },
                         error:function(jqXHR, textStatus, errorThrown) {
                            console.log('request failed ' + textStatus + errorThrown);
                            console.log(appDetails);
                         }
                        });
                    }
                });
            </script>

对于第一个URL,以及我正在抓取数据的大约一半的URL,一切都很好。问题是通过ajax发送的一些数据返回以下错误

Upload.php net::ERR_EMPTY_RESPONSE

你们有人能帮忙吗?

试试这个。在以json形式发布详细信息之前,必须对其进行字符串化。这可能是ERR_EMPTY_RESPONSE的原因。

function callAjax(appDetails) {
                            appDetailsJsonString = JSON.stringify(appDetails);
                            $.ajax({
                             url: 'Upload.php',
                              type: 'POST',
                              data: {appDetailsJson: appDetailsJsonString},
                              dataType: 'json',
                             success:function(data) {
                                    console.log(data);
                               },
                             error:function(jqXHR, textStatus, errorThrown) {
                                console.log('request failed ' + textStatus + errorThrown);
                                console.log(appDetails);
                             }
                            });
                        }