Другое решение этого состояло бы в том, чтобы не писать калькулятор как этот в целом. Запись синтаксического анализатора RPN намного более проста, и не имеет ни одной неоднозначности свойственной от записи математики с инфиксной нотацией.
import operator, math
calc_operands = {
'+': (2, operator.add),
'-': (2, operator.sub),
'*': (2, operator.mul),
'/': (2, operator.truediv),
'//': (2, operator.div),
'%': (2, operator.mod),
'^': (2, operator.pow),
'**': (2, math.pow),
'abs': (1, operator.abs),
'ceil': (1, math.ceil),
'floor': (1, math.floor),
'round': (2, round),
'trunc': (1, int),
'log': (2, math.log),
'ln': (1, math.log),
'pi': (0, lambda: math.pi),
'e': (0, lambda: math.e),
}
def calculate(inp):
stack = []
for tok in inp.split():
if tok in self.calc_operands:
n_pops, func = self.calc_operands[tok]
args = [stack.pop() for x in xrange(n_pops)]
args.reverse()
stack.append(func(*args))
elif '.' in tok:
stack.append(float(tok))
else:
stack.append(int(tok))
if not stack:
raise ValueError('no items on the stack.')
return stack.pop()
if stack:
raise ValueError('%d item(s) left on the stack.' % len(stack))
calculate('24 38 * 32 / 2 +')
What is the nature of Service 'A' and 'B'? A neater solution would be to handle the discrepancy server-side. For example, if Service A is a PHP script...
<?php
if(prerequisite for display service A plain text){
echo "service a plain text";
}else{
echo "service b plaint text";
}
?>
Alternatively, if these services are text files, you could have a third file that makes the decision, and includes Service A or B depending. For example, this could be ajax.php, and you would call ajax.php in all cases:
<?php
if(prerequisite for displaying service A plain text){
echo file_get_contents("a.txt");
}else{
echo file_get_contents("b.txt");
}
?>
From what it sounds like, your conditional will be file_exists("a.txt")
, but that's just a guess on my part.
Good luck, and comment if you've any questions!
Я не могу доказать, но надеюсь, что этот сценарий может помочь вам найти решение:
вам нужно будет подтвердить различия в вашем статусе или текст для каждого типа ответа от "a. php "
$.ajax({
type: "GET",
url: "a.php",
complete: function (XMLHttpRequest, textStatus) {
if (XMLHttpRequest.status!=200) // or responseText
{
var fn = arguments.callee;
var _this = this;
setTimeout(function(){fn.call(_this, XMLHttpRequest, textStatus);}, 200);
}
else
{
//ok
}
}
});
или EDIT:
complete: function xCompleteFunction(XMLHttpRequest, textStatus) {
if (XMLHttpRequest.status!=200) // or responseText
{
var _this = this;
setTimeout(function(){xCompleteFunction.call(_this, XMLHttpRequest, textStatus);}, 200);
}
else
{
//ok
}
}
EDIT II:
redirect.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title></title>
<script type="text/javascript">
$(function(){
$("#senddata").click(function(){
$.ajax({
type: "GET",
url: "a.php",
complete: function xCompleteFunction(XMLHttpRequest, textStatus) {
$("#info").append(""+XMLHttpRequest.status+"<br />"+XMLHttpRequest.responseText+"<br>");
if (XMLHttpRequest.status==301) // or responseText
{
var _this = this;
setTimeout(function(){xCompleteFunction.call(_this, XMLHttpRequest, textStatus);}, 200);
$("#info").append("waiting redirect<br>");
}
else
{
$("#info").append("redirect ok<br>");
}
}
});
});
});
</script>
</head>
<body>
<button id="senddata">send ajax request</button>
<pre id="info"></pre>
</body>
</html>
a.php:
<?php
for($a=0;$a<1000000;$a++)
{
//wait
}
header('Location: b.php');
b.php:
<?php
print "hola mundo";
Важно: Определения кодов состояния
Индикаторы прогресса в веб-контенте сложно получить правильно, потому что вы не знаете общее время, которое займет процесс, и у вас нет индикаторов прогресса процесса, чтобы вы знали, когда обновлять полоску. Возможно, вам лучше использовать вращающееся изображение загрузки, подобное тем, которые доступны на ajaxload.com .
Вы можете сделать скрытый div, покрывающий всю страницу, и активировать div в своем javascript, когда вы ждете .
В вашем html: