dongzuozhu66776 2014-07-28 20:17
浏览 53
已采纳

您是否应该在Ajax调用中更新数据库?

I'm running into an issue of where I would like to update a database after an ajax call has been fully made. In order to find the value that I want to update the database with I have to make a SOAP call (or server request) which take some time. I'd like to instead use the value that was found in that ajax call and then store that in my database.

Here is my ajax call

 <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> 

and here is the balance.php that is being called

    /**
*@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);
}

Now would it be appropriate to then update my database from within balance.php? I'd like to keep my database updated - and I don't know a way to call a function like " updatDatabase" i in the controller after the render because it will JS run before the java script function finishes.

  • 写回答

1条回答 默认 最新

  • dtpk04526211 2014-07-28 20:20
    关注

    Well technically speaking you can't update the DB in the AJAX call... your AJAX call fires code and an AJAX call is not much different at all than any other HTTP method. I mean it is a HTTP call... so of course it is perfectly fine to update the DB if the code being fired is a result of an AJAX call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条