我目前正在嘗試編寫一些JavaScript與我使用XMXMLHttpRequest()在GAE(使用Python)上部署的API進行交互。我沒有獲得GET的問題,但是PUT給我帶來了很多麻煩。使用XMLHttpRequest()PUT與GAE Python
有趣的是,我從測試HTTP站點(https://www.hurl.it/)觸摸PUT請求沒有問題,但是每次嘗試從我自己的Javascript代碼中收到狀態值0。以下是我的GAE和Javascript代碼的片段。
(注意 - 我必須用一個「放」該呼叫的要求)
任何指導,將不勝感激!
GAE(服務器):
def put(self):
# Save variables for update
cardkey = self.request.get('key', default_value=None)
ident = self.request.get('ident', default_value=None)
brand = self.request.get('brand', default_value=None)
year = self.request.get('year', default_value=None)
player = self.request.get('player', default_value=None)
# If card key is provided then update card
if cardkey:
# Get card
card_to_update = ndb.Key(db_models.Card, int(cardkey)).get()
if ident:
card_to_update.ident = ident
if brand:
card_to_update.brand = brand
if year:
card_to_update.year = year
if player:
card_to_update.player = player
# Save changes and print update to requester
card_to_update.put()
card_dict_format = card_to_update.to_dict()
self.response.write(json.dumps(card_dict_format))
return
# If card key is not provided send error
else:
self.response.write('key not provided. must provide key for update.')
return
而且從我的網頁上的JavaScript:
<script>
window.onload = function()
{
var myRequest = new XMLHttpRequest();
var url = 'http://cs496-assignment3-mastrokn.appspot.com/updatecard';
var param = 'key=5636318331666432';
myRequest.open('put', url);
myRequest.onreadystatechange = function()
{
if ((myRequest.readyState == 4) && (myRequest.status == 200))
{
// var myArr = JSON.parse(myRequst.responseText);
// myFunction(myArr);
document.getElementById("viewCards").innerHTML = myRequest.status;
}
else
{
document.getElementById("viewCards").innerHTML = myRequest.status;
}
}
myRequest.send(param);
}
</script>