2012-03-08 25 views
0

我試圖在成功調用Realex(處理信用卡事務的公司)後清除一些會話變量。會話變量包含我的購物車內容,所以一旦交易成功,我想清除它。我的代碼如下:Ajax調用不按預期方式工作

jQuery.ajax({ 
        async: false,          
        url: templateuri+'/realex_go_pay.php',     
        type: 'post', 
        data: {               
         amount: ordertotal, 
         orderid: orderid, 
         orderkey: orderkey, 
         cardtype: type, 
         cardnumber: number, 
         cardname: name, 
         expdate: expiry, 
         cvv: cvv, 
         issueno: issueno 
        }, 
        success: function(data){       
         // Hide payment indicator. 
         jQuery('.realex_payindicator').hide(); 
         data = data.split("|"); 
         if (data[0] == '00'){                                   
          // Empty the shopping cart. 
          jQuery.ajax({ 
           async: false,                  
           url: templateuri+'/realex_empty_cart.php'                
          }); 
          self.location = thankyoupage+'?order='+orderid+'&key='+orderkey;                        
         }else{ 
          alert(data);        
         }      
        } 
       });     

第一次調用realex_go_pay.php工作沒有問題。但第二次調用realex_empty_cart.php似乎沒有工作。在realex_empty_cart.php,我只有兩行代碼:

unset($_SESSION['cart']); 
unset($_SESSION['coupons']); 
+1

你有螢火蟲或類似的東西檢查?是否已經發出了Ajax請求? realex_empty_cart.php是否成功響應? – arunes 2012-03-08 20:45:01

+0

確保你在php文件中回覆「成功」消息 – adamdehaven 2012-03-08 20:46:53

+1

使第二個Ajax調用也是一個帖子並添加'data:{}' – Archer 2012-03-08 20:47:48

回答

0

我覺得弓箭手在他的評論很好的解決,但原因可能是處理緩存。默認情況下,jQuery.ajax將允許瀏覽器緩存GET請求的響應。這意味着它不會與服務器通信。添加

cache: false 

到您的代碼中的選項強制它去服務器。

success: function(data){       
    // Hide payment indicator. 
    jQuery('.realex_payindicator').hide(); 
     data = data.split("|"); 
     if (data[0] == '00'){                                   
     // Empty the shopping cart. 
      jQuery.ajax({ 
       async: false,                  
       cache: false, 
       url: templateuri+'/realex_empty_cart.php'                
      }); 
      self.location = thankyoupage+'?order='+orderid+'&key='+orderkey;                        
     }else{ 
      alert(data);        
     }      
    } 

(見http://api.jquery.com/jQuery.ajax/獲取更多信息)

歐文