weixin_33726313 2014-03-26 19:43 采纳率: 0%
浏览 39

AJAX POST请求失败

Apologies for the generic title.

Essentially, when the script runs 'error' is alerted as per the jQuery below. I have a feeling this is being caused by the structuring of my JSON, but I'm not sure how I should change it.

The general idea is that there are several individual items, each with their own attributes: product_url, shop_name, photo_url, was_price and now_price.

Here's my AJAX request:

$.ajax(
{
    url : 'http://www.comfyshoulderrest.com/shopaholic/rss/asos_f_uk.php?id=1',
    type : 'POST',
    data : 'data',
    dataType : 'json',
    success : function (result)
    {
        var result = result['product_url'];
        $('#container').append(result);
    },
    error : function ()
    {
       alert("error");
    }
})

Here's the PHP that generates the JSON:

<?php

function scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country)
{
    header("Access-Control-Allow-Origin: *");

    $html = file_get_contents($list_url);
    $doc = new DOMDocument();
    libxml_use_internal_errors(TRUE);

    if(!empty($html))
    {
        $doc->loadHTML($html);
        libxml_clear_errors(); // remove errors for yucky html
        $xpath = new DOMXPath($doc);

        /* FIND LINK TO PRODUCT PAGE */

        $products = array();

        $row = $xpath->query($product_location);

        /* Create an array containing products */
        if ($row->length > 0)
        {            
            foreach ($row as $location)
            {
                $product_urls[] = $product_url_root . $location->getAttribute('href');
            }
        }

        $imgs = $xpath->query($photo_location);

        /* Create an array containing the image links */
        if ($imgs->length > 0)
        {            
            foreach ($imgs as $img)
            {
                $photo_url[] = $photo_url_root . $img->getAttribute('src');
            }
        }

        $was = $xpath->query($was_price_location);

        /* Create an array containing the was price */
        if ($was->length > 0)
        {
            foreach ($was as $price)
            {
                $stripped = preg_replace("/[^0-9,.]/", "", $price->nodeValue);
                $was_price[] = "&pound;".$stripped;
            }
        }


        $now = $xpath->query($now_price_location);

        /* Create an array containing the sale price */
        if ($now->length > 0)
        {
            foreach ($now as $price)
            {
                $stripped = preg_replace("/[^0-9,.]/", "", $price->nodeValue);
                $now_price[] = "&pound;".$stripped;
            }
        }

        $result = array();

        /* Create an associative array containing all the above values */
        foreach ($product_urls as $i => $product_url)
        {
            $result = array(
                'product_url' => $product_url,
                'shop_name' => $shop_name,
                'photo_url' => $photo_url[$i],
                'was_price' => $was_price[$i],
                'now_price' => $now_price[$i]
            );
            echo json_encode($result);
        }
    }
    else
    {
        echo "this is empty";
    }
}

/* CONNECT TO DATABASE */

$dbhost = "xxx";
$dbname = "xxx";
$dbuser = "xxx";
$dbpass = "xxx";

$con = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$dbname");

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_GET['id'];

/* GET FIELDS FROM DATABASE */

$result = mysqli_query($con, "SELECT * FROM scrape WHERE id = '$id'");

while($row = mysqli_fetch_array($result))
{   
    $list_url = $row['list_url'];
    $shop_name = $row['shop_name'];
    $photo_location = $row['photo_location'];
    $photo_url_root = $row['photo_url_root'];
    $product_location = $row['product_location'];
    $product_url_root = $row['product_url_root'];
    $was_price_location = $row['was_price_location'];
    $now_price_location = $row['now_price_location'];
    $gender = $row['gender'];
    $country = $row['country'];
}

scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country);

mysqli_close($con);

?>

The script works fine with this much simpler JSON:

{"ajax":"Hello world!","advert":null}
  • 写回答

3条回答 默认 最新

  • csdn产品小助手 2014-03-26 19:46
    关注

    You are looping over an array and generating a JSON text each time you go around it.

    If you concatenate two (or more) JSON texts, you do not have valid JSON.

    Build a data structure inside the loop.

    json_encode that data structure after the loop.

    评论

报告相同问题?

悬赏问题

  • ¥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,如何解決?
  • ¥15 c++头文件不能识别CDialog