簡體   English   中英

您是否應該在Ajax調用中更新數據庫?

[英]Should you ever update your database in an Ajax call?

我遇到了一個問題,我想在完全執行ajax調用在哪里更新數據庫。 為了找到我要用來更新數據庫的值,我必須進行一些SOAP調用(或服務器請求)。 我想改用在該ajax調用中找到的值,然后將其存儲在我的數據庫中。

這是我的ajax電話

 <script>
function getBalance(){
    $.get("assets/balance.php", "", function(data){
        // # means an id, but a . would mean a class
        // .html means replace the html of id with the balance 
        $('#balance').html(data);
        // alert(data);
    });
}
getBalance();

</script> 

這是被稱為balance.php的

    /**
*@method CheckBalance() : this  method helps to get the balance info of the tigo cash subscriber using tigo rwanda middleware
*@param string  $msisdn : this is the mobile number of the tigo cash subscriber
*@param string $pin    : this is the pin number of the tigo cash account 
*@return returns the decoded answer either as the balance (int) or a warning (string)
*/
function BalanceCall($msisdn,$pin){

  //Store your XML Request in a variable
    $input_xml = 


.... some xml .... 

// url of the server the request is going to  
$url = "http://10.138.84.138:8002/osb/services/GetBalance_1_0";



// returns a long xml string reply
$xmlstring = curl_exec($soap_do);

// this returns either the balance (int) or an error (string)
return $result = Helpers::decodeBalanceString($xmlstring);
}

現在從balance.php中更新我的數據庫是否合適? 我想保持數據庫更新-而且我不知道一種在渲染后在控制器中調用“ updatDatabase”之類的函數的方法,因為它將在Java腳本函數完成之前運行JS。

從技術上講,您無法在AJAX調用中更新數據庫...您的AJAX調用會觸發代碼,並且AJAX調用與任何其他HTTP方法都沒有太大不同。 我的意思是這是一個HTTP調用...因此,如果要觸發的代碼是AJAX調用的結果,則當然可以更新數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM