我有以下代码,它接受来自Cardspring服务器的响应,并尝试将其发送到我们的网站以存储在我们的数据库中。我无法让ajax正常工作。当我观察heroku服务器日志时,似乎ajax没有向我们的服务器发送POST - ajax请求没有日志条目。这是ajax代码的样子:
<script type="text/javascript">
$(document).ready(function()
{
$('#contactform').submit(function(event) {
$('.submit-button').attr('disabled', 'disabled');
cardSpringResponseHandler = function(response){
if((typeof response.error) != "undefined")
{
var error = response.error;
var reason = response.reason;
alert(error + ' ' + reason);
}
else
{
alert(response.token + ' ' + response.last4 + ' '+ response.brand + ' ' + response.brand_string + ' ' + response.expiration);
alert('Thank you, your card is registered!');
$.ajax({
type: 'POST',
url:'www.url.com/me',
async: false,
data: {"token": response.token,"last4": repsonse.last4,"brand": response.brand,"brand_string":response.brand_string,"expiration": response.expiration}.serialize(),
error: function () {
alert('Error');
}
});
}
alert('Hello 2');
};
alert('adding card');
CardSpring.addCard(cardSpringResponseHandler
, {
publisher_id : '{{ CARDSPRING_APP_ID }}',
security_token : '{{ securityToken }}',
timestamp : '{{ timestamp }}',
hmac : '{{ digestedHash }}'
}, {
card_number : $('.card-number').val(),
exp_month : $('.expiration-month').val(),
exp_year : $('.expiration-year').val(),
user_id : '{{ csid }}'
}, "all");
alert('card added');
return false;
});
});
</script>
这是urls.py的一个片段,用于显示网址:
url(r'^me/$', accountInfo),
这是服务器端代码的样子,作为django视图:
def accountInfo(request):
#after CS.addCard is sucessful, this adds the credit card Token to our DB...
print request.method
if request.method == "POST":
print "in account info"
print "expiration = " ,
#print request.POST['expiration']
print "request ="
print request
try:
cardToAdd = Cards(csID = request.COOKIES.get('csID'),token = request.POST['token'], last4 = request.POST['last4'], cardType = request.POST["brand"] ,typeString = request.POST['brand_string'],
expDate = request.POST['expiration'])
cardToAdd.save();
json_data = json.dumps({"HTTPRESPONSE":"sucess"})
return HttpResponse(json_data, mimetype="application/json")
except:
json_data = json.dumps({"HTTPRESPONSE":"fail"})
return HttpResponse(json_data, mimetype="application/json")
初始打印request.method在第一次加载页面时运行,方法= GET。服务器不打印任何其他请求,这使我认为ajax代码没有运行
警报(至少其中一些)确实发生了。它们总是按此顺序出现:
有谁知道发生了什么事?谢谢你的帮助 - 我对ajax和javascript非常生疏