第一次发表海报,要温柔。无论如何,我会尝试尽可能清楚。
我有一个网站,它使用jQuery / Ajax和PHP的组合来创建优惠券。当用户访问时,他们选择他们想要购买的优惠券(通过PayPal)并提交。我发送给paypal并在我的PHP脚本中监听IPN,在有效IPN的情况下,脚本应该继续为他们选择的优惠券分配一个随机生成的12位“条形码”,将它们全部添加到文件中打开打印。我正在使用this jQuery plugin
存储选定的优惠券数据(名称数组)最初的优惠券生成工作,paypal集成(包括IPN监听器)工作。我在测试时可以在控制台中看到cookie,因此我的表单数据就在那里。我甚至得到了json数组的“成功”部分。
我的问题是我正在尝试通过ajax获取图像文件,但我收到'SyntaxError:JSON.parse:意外的数据结束',我无法弄清楚原因。在我开始使用之前已经编写了图像创建代码,我只需要将它与paypal结合在一起。
我的-offers.php
$(window).load(function(){
var extradata = {};
extradata.action = 'createCoupon';
extradata.offers = $.cookies.get("offers");
console.log(extradata);
$.ajax({
url: 'api/data_request.php',
success: function(e, data, result){
console.log('done!', data);
//this returns 'success' but console.log('done!', e) returns empty string
//below is throwing unexpected end of data
var resultObj = JSON.parse(e);
if(resultObj.result === 'success'){
//console.log('success!'+resultObj);
window.open(resultsObj.filename, '_blank');
}else{
console.log('some sort of error!', data);
}
},
type: 'POST',
data: extradata,
error: function(e, data, extra){
console.log('there was a serious error!', e, data, extra);
console.log(data.errorThrown, data.textStatus, data.jqXHR);
}
});
数据request.php
//go through finished coupons array, and put them all on one image
//then save the image in coupons
$finalimg = imagecreatetruecolor($thewidth, $theheight);
$cursor = 0;
for($i = 0; $i<4; $i++){
$obj = $finishedCoupons[$i];
$coupon = $obj[0];
$width = $obj[1];
$height = $obj[2];
$copyied = imagecopymerge($finalimg, $coupon, 0, $cursor, 0, 0, $width, $height, 100);
if($copyied === false){
returnError('error copying images');
}
$cursor = $cursor+$height;
}
$didit = imagejpeg($finalimg, '/images/coupons/'.$thefilename.'.jpg', 100);
if($didit === false){
returnError('final copying error'.$thefilename);
}else{
echo json_encode(array('result'=>'success', 'filename'=>'images/coupons/'.$thefilename.'.jpg'));
die();
}
MySQL选择优惠券并创建条形码(用于上下文)
//get the coupon picture we'll put the numbers on
$success1 = $mysqli->prepare("SELECT title, text, realcoupon FROM offers WHERE offer_id = ? LIMIT 1");
$success1->bind_param('i', $coupon);
$success1->execute();
$success1->store_result();
$success1->bind_result($title, $text, $source);
$success1->fetch();
//now actually generate the numbers
//perform database collision detection
//number is 12 digits long so we can use it in a barcode if needed
$exists = 1;
while($exists !== 0){
$code = generateRandomNumber();
$success1 = $mysqli->prepare("SELECT barcode FROM offerId WHERE barcode = ?");
$success1->bind_param('s', $code);
$success1->execute();
$success1->store_result();
$exists = $success1->num_rows;
}
$thefilename.=$code;
//$code is a unique barcode now, so we should insert it into the array
// coupon contains the offer_id
$success = $mysqli->prepare("INSERT INTO offerId (offer_id, barcode, date) VALUES (?, ?, CURRENT_TIMESTAMP)");
$success->bind_param('is', $coupon, $code);
$success->execute();
//$success->store_result();
//$success->fetch();
$affected = $success->affected_rows;
if($affected !== 1){
returnError($affected);
returnError('Error inserting the barcode!');
}
//now put the numbers on the coupon and push it to the finishedCoupons array
$source = '../images/'.$source;
try{
$extension = pathinfo($source, PATHINFO_EXTENSION);
if(!file_exists($source)){
returnError('Cant find coupon!');
return false;
}
$info = getimagesize($source);
if($info === false){
returnError("InvalidImage");
return false;
}
}catch (Exception $e){
returnError("InvalidImage");
return false;
}
$type = $info[2];
$width = $info[0]; // you don't need to use the imagesx and imagesy functions
$height = $info[1];
if($width>$thewidth){
$thewidth = $width;
}
我已经尝试将php文件设置为header('Content-type:application / json'); 我试过在我的ajax调用中调用dataType:'json'。我甚至运行php -l data_request.php来检查语法错误,没有什么是returend。似乎没什么用。这是我需要调试的最后一步!!我已经工作了好几个小时了。
更新:检查响应标题后,看起来它正在被转换为text / html类型而不是text / json或application / json
答案 0 :(得分:0)
PHP的最后一行是问题:
echo json_encode(array('result'=>'success',
'filename'=>'images/coupons/'.$thefilename.'.jpg'));
die(); echo $coupons;
当您编写PHP服务时,您应该只回显JSON,否则JSON响应数据将无效,因为它与另一个字符串连接。
您必须只回显JSON
:
echo json_encode(array('result'=>'success',
'filename'=>'images/coupons/'.$thefilename.'.jpg'));
die(); //echo $coupons;
答案 1 :(得分:0)
发现了这个问题。
IPN不是实时的,因为这里的客户端需要在支付(ipn)失败的情况下中断支付流程,这不起作用,这反过来导致JSON部分为空,因为它用paypal IPN包装在if()中。所以我用paypal PDT(支付数据传输)将其更改为即时,然后根据交易ID和付款金额进行验证。