我遇到了一个问题,即在 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 中更新我的数据库是否合适?我想保持我的数据库更新 - 而且我不知道如何在渲染后调用 Controller 中的“updatDatabase”之类的函数,因为它将在 java 脚本函数完成之前运行 JS。
最佳答案
从技术上讲,您无法在 AJAX 调用中更新数据库...您的 AJAX 调用会触发代码,并且 AJAX 调用与任何其他 HTTP 方法没有太大区别。我的意思是这是一个 HTTP 调用...因此,如果触发的代码是 AJAX 调用的结果,那么更新数据库当然是完全可以的。
关于javascript - 您是否应该在 Ajax 调用中更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25003584/