简体   繁体   English

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

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

I'm running into an issue of where I would like to update a database after an ajax call has been fully made. 我遇到了一个问题,我想在完全执行ajax调用在哪里更新数据库。 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. 为了找到我要用来更新数据库的值,我必须进行一些SOAP调用(或服务器请求)。 I'd like to instead use the value that was found in that ajax call and then store that in my database. 我想改用在该ajax调用中找到的值,然后将其存储在我的数据库中。

Here is my ajax call 这是我的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> 

and here is the balance.php that is being called 这是被称为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);
}

Now would it be appropriate to then update my database from within balance.php? 现在从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. 我想保持数据库更新-而且我不知道一种在渲染后在控制器中调用“ updatDatabase”之类的函数的方法,因为它将在Java脚本函数完成之前运行JS。

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. 从技术上讲,您无法在AJAX调用中更新数据库...您的AJAX调用会触发代码,并且AJAX调用与任何其他HTTP方法都没有太大不同。 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. 我的意思是这是一个HTTP调用...因此,如果要触发的代码是AJAX调用的结果,则当然可以更新数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM