Why my ajax failed?

Hi guys!

I am writing a program in ajax and php,
my ajax can pass the data to my php, and my php also can receive and save the data to my DB.

But, my problem is the ‘error: function ()’ of my ajax always be fried.
In other words, I can’t do something to send a response to my frontend after my ajax has been successfully completed,
Could you please tell me why?

My ajax:

function SetAdvisePrice(){
	if ($("#AdvisePrice").find("option:selected").val()) {
		$.ajax({
			url: WebsitePath + '/settings',
			data: {
				AdvisePrice: $("#AdvisePrice").find("option:selected").val()
			},
			type: 'POST',
			dataType: 'json',
			success: function (Result) {
			if (Result.Status == 1) {
			console.log('OK!');
			//do something
			} 
			else {
				console.log('Failed!');
			}
		},
		error: function () {
			console.log('Failed!');
                        //always stuck in here
		}
		
	});
}
}

My php:

$AdvisePrice = intval(Request('Post', 'AdvisePrice'));
$MerchandiseResult = $AdvisePrice;
if (empty($MerchandiseResult)) {
$MerchandiseResultMessage = '商品信息没有被修改';
}
else {
		try {
		$DB->beginTransaction();
		$DB->query('UPDATE ' . PREFIX . 'users SET AdvisePrice = :AdvisePrice WHERE ID = :ID',
		array(
		"AdvisePrice" => $MerchandiseResult,
		"ID" => $CurUserInfo['ID']
		 ));
		$DB->commit();
		} catch (Exception $ex) {
			$DB->rollBack();
			return false;
		}
		echo json_encode(array('Status'=>'1'));
	    //$DB->CloseConnection();
        
		$MerchandiseResultMessage = '商品信息设置成功';
		}

I don’t know is it safty, but I did this:

error: function (jqXHR, exception) {
         //console.log(jqXHR.statusText);
		 if(jqXHR.statusText== 'OK'){
			 alert('商品设置成功。' + '您目前的咨询费是' + AdvisePrice + '元半小时。');
		 }

It seems to resolved my problem temporarily.

1 Like