javascript - AJAX POST 请求失败

标签 javascript php jquery ajax json

对通用标题表示歉意。

本质上,当脚本运行时,会根据下面的 jQuery 发出“错误”警报。我感觉这是由 JSON 的结构引起的,但我不确定应该如何更改它。

总体思路是有几个单独的项目,每个项目都有自己的属性:product_urlshop_namephoto_url was_pricenow_price

这是我的 AJAX 请求:

$.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");
    }
})

这是生成 JSON 的 PHP :

<?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);

?>

该脚本可以很好地处理这个简单得多的 JSON:

{"ajax":"Hello world!","advert":null}

最佳答案

您正在循环遍历一个数组并生成一个 JSON 文本每次遍历它时

如果您连接两个(或更多)JSON 文本,则您没有有效的 JSON。

在循环内构建数据结构。

json_encode 循环之后的数据结构。

关于javascript - AJAX POST 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22671480/

相关文章:

php - PHP 中的 Schinke 拉丁词干提取算法

php - 使用 jquery php 自动发送电子邮件通知

jquery - 暂停每个淡入淡出循环

javascript - 简单的BIRT表达式问题

java - 如何在 JavaScript 中实现这个 java 模式(使用继承)?

php - 使用 PHP 导出 excel 时编码字符串错误

javascript - DOM 准备好后,onLoad 总是会被触发吗?

javascript - "$"在 Visual Studio 中使用 jQuery 时未定义

javascript - 没有特定子项的 Protractor 过滤器元素

javascript - 在一个页面上有两个 Javascript,另一个阻止另一个工作