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 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题