使用 jQuery,php,mysql ,我编写了获取 ueprice 的程序:
(1)成功获取价格后
$prices = array();
$row=mysql_fetch_row($res);
$ueprice = intval($row[0]);
$pricetotal = $ueprice * $suryou;
$selprice = round($pricetotal*$crate);
$prices =array(
'unitp' => $ueprice, 'sump'
=> $pricetotal,
'sellp' => $selprice
);
(2) 使用 json_encode header('Content-Type: application/json')
返回 $prices ;
回声 json_encode($prices);
出口;
(3)调用php的javascript是:
$(function(){
setPrices1d();
$("#selectQuantity1d").on("change", setPrices1d);
});
function setPrices1d() {
$(document).ready(function()
{
var grn = $('#main1d').val(); //get rank
var ghn = $('#sub1d option:selected').text(); //get hinban
var gkn = $('#detail1d').val(); //get kinou
var gch = $('#curtainH1d').val(); //get curtain height
var gcw = $('#curtainW1d').val(); //get curtain width
var gcr = $('#sellingRate1d').val(); //get customer_rate
var gsu = $('#selectQuantity1d').val(); //get suryou
var q = encodeURIComponent("drape");
var url = 'jsonprice.php?rank=' + grn + '&categ=' + q + '&hinban='
+ ghn + '&kinou=' + gkn + '&curtainh=' + gch +
'&curtainw=' + gcw + '&crate=' + gcr + '&suryou=' + gsu;
$.ajax({
type: "GET",
url: url,
dataType: "json",
/**
* Ajax通信が成功した場合に呼び出されるメソッド
*/
success: function(data) {myObject=JSON.parse(data);}
{
//結果が0件の場合
if(data == null) alert('データが0件でした');
//返ってきたデータの表示
var $unitprice = $('#unitPrice1d');
var $sumprice = $('#sumTotal1d');
var $sellprice = $('#sellingPrice1d');
// clear input area
$unitprice.empty();
$sumprice.empty();
$sellprice.empty();
//set prices into text area
$unitprice.val(data[0].unitp);
$sumprice.val(data[0].sump);
$sellprice.val(data[0].sellp);
},
/**
* Ajax通信が失敗場合に呼び出されるメソッド
*/
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('エラー : HttpReq= ' + XMLHttpRequest + ' textStatus =' + textStatus + ' errThrown =' + errorThrown + ' errThrown.message =' + errorThrown.message + ' HttpReq.status =' + XMLHttpRequest.status);
}
});
});
}
(4)以上$.ajax返回如下错误:HttpReq= [object Object] errThrown = SyntaxError: Unexpected token a Httpreq.status = 200
(5) 为了弄清楚这个错误,我花了几天时间,但还没有解决。使用谷歌浏览器,网络响应在我看来不错: Network Resonponce array(8) { ["rank"]=>
string(1) "S" ["categ"]=> string(12) "drape" ["hinban "]=> string(7) "TKR8001" ["kinou"]=> string(10) "2倍ヒダ" ["curtainh"]=> string(6) "45-120" ["curtainw"]=> string(5) "50-75" ["crate"]=> string(4) "0.90" ["suryou"]=> string(1) "3" }
{"unitp":30000,"sump":90000,"sellp":81000}
我非常感谢您对此解析错误的建议,非常感谢!