weixin_33728268 2015-08-13 09:16 采纳率: 0%
浏览 37

循环时的Ajax错误

Im using a mixture of php and javascript/jquery to scrape a websites prices from there webpage, there's no API so unfortunately I scrape the html page and pick up the prices/title from the html(I know this is a really risky way of doing it but its the only way).

Anyway, this is whats going on:

I pull in the external webpage using php file_get_contents()

This method is within a foreach loop, for every external page I want to grab data from.

This is my 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>

Everything works fine for the first URL, and for about half of the urls that I'm grabbing data from. The issue is SOME of the data being sent via ajax is returning the following error

Upload.php net::ERR_EMPTY_RESPONSE

Can any of you help?

  • 写回答

1条回答 默认 最新

  • 程序go 2015-08-13 12:15
    关注

    Try this. You must stringify your details before you post them as json. This could be a reason for 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);
                                 }
                                });
                            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?