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);
                                 }
                                });
                            }
    
    评论

报告相同问题?