[英]Should you put HTML code in your JavaScript or call it from the server?
[英]Should you ever update your database in an Ajax call?
我遇到了一个问题,我想在完全执行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中更新我的数据库是否合适? 我想保持数据库更新-而且我不知道一种在渲染后在控制器中调用“ updatDatabase”之类的函数的方法,因为它将在Java脚本函数完成之前运行JS。
从技术上讲,您无法在AJAX调用中更新数据库...您的AJAX调用会触发代码,并且AJAX调用与任何其他HTTP方法都没有太大不同。 我的意思是这是一个HTTP调用...因此,如果要触发的代码是AJAX调用的结果,则当然可以更新数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.