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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分