ajax请求响应错误数据

时间:2014-01-21 16:57:09

标签: javascript php ajax jquery

我有一个页面,我想每分钟刷新div而不刷新整个页面.Div从另一个xml文件中计算更大价格的php文件中获取数据。我读到最好的方法是ajax请求。

我的ajax代码:

<script type="text/javascript">
$(function(){
var name = $("#product").val();
  $.ajax({
    type: "GET",
    url: "maxPrice.php",
    dataType: "text",
    data: {'product_name' : name}, 
    error: function(XMLHttpRequest, textStatus, errorThrown) {
     alert(textStatus);
     },
    success:function(result) {
        alert(result);
    }
  });
});
</script>

php文件:

<?php
$product_name = $_GET['product_name'];

    $loop = 0;
    $auction_price_db = 0;
    $id_product = id_from_product_name($product_name);
    $doc = new DOMDocument();
    $doc->load('C:/wamp/www/store/connections/auctions.xml');
    $xpath = new DOMXPath($doc);

    $xml_offers = simplexml_load_file('C:/wamp/www/store/connections/offers.xml');
    $xml_auctions = simplexml_load_file('C:/wamp/www/store/connections/auctions.xml');

    foreach ($xml_auctions->auction as $auction){
         if ($auction->auction_id == $id_product) {
             $auction_price_db = (string)$auction->start_price;
             $item = $loop;
             break;
         }
         $loop++;
    }

      foreach ($xml_offers->offer as $offer){
         if ($offer->id_product == $id_product) {
             $user_offer = (string)$offer->price;
             if ($user_offer > $auction_price_db) {
                 $parent = $doc->getElementsByTagName('auction')->item($item);
                 $query = $xpath->query('start_price',$parent);
                 $query->item(0)->nodeValue = $user_offer;
                 $doc->save('C:/wamp/www/store/connections/auctions.xml');
                 $xml_products = simplexml_load_file('C:/wamp/www/store/connections/products.xml');
                 foreach ($xml_products->product as $product){
                       if ($id_product == $product->product_id) {
                           $product->price = $user_offer;
                           $xml_products->asXML('connections/products.xml');
                       }
                 }
                $response =  "{\"user_offer\":\"".$user_offer."\"}";
             }
         }
    }
echo <<<HERE_DOC
$response
HERE_DOC;

?>

在ajax的成功中,我有一个警告,看看来自ajax的数据是否正确。但是它回应了这个..

<br />
<font size='1'><table class='xdebug-error xe-fatal-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Call to undefined function id_from_product_name() in C:\wamp\www\store\maxPrice.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>692568</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\store\maxPrice.php' bgcolor='#eeeeec'>..\maxPrice.php<b>:</b>0</td></tr>
</table></font>

为什么我得到这个回复?如果我尝试删除maxPrice中的所有代码并仅回显我在ajax请求中传递的$ product_name,我会得到正确的数据。

1 个答案:

答案 0 :(得分:0)

这是来自服务器的Xdebug格式错误。如果仔细观察第3行,您将看到来自服务器的错误消息:

Fatal error: Call to undefined function id_from_product_name() in C:\wamp\www\store\maxPrice.php on line 6