[英]Why is this jquery script not working?
因此,我有一個網站,允許用戶輸入鏈接,然后提取信息並將其放入他們的列表中。
現在一切正常,直到昨天我的網絡托管合作伙伴完成了一些升級。 我知道mysql和php一直在升級,但不知道還有什么。
首先,我遇到了無法登錄數據庫的問題(不得不刪除並重新創建用戶)。 然后是PHP序列化JSON的問題(需要更改代碼)。 然后是412個無效的前提條件錯誤(需要由托管伙伴設置的特殊規則)。 現在,最后一個jquery腳本已停止工作。 但是我不知道為什么。 它曾經工作過,但也許很幸運(我對此並不那么有經驗)。
反正會發生什么
使用firebug並在我的javascript中放置警報,然后可以看到步驟1,步驟2正常運行。 返回的JSON有效(我已使用JSONlint對其進行了驗證),但第3步無效。 我的腳本在下面;
function getURLData(form)
{
var listid = $('input#listid').val();
var memberid = $('input#memberid').val();
var link = $('input#link').val();
var sendstr = "http://www.wishindex.com/ajax.php?link=\"" + link + "\"&listid=" + listid + "&memberid=" + memberid;
alert(sendstr);
$.getJSON(sendstr,function(result)
{
alert('inside');
if(result['Itemname'] != "")
{
alert('inside2');
$('#itemname').val(result['Itemname']);
$('#itemname').attr('readonly', 'true');
$('#desc').val(result['Description']);
$('#category').val(result['Category']);
$('#category').attr('readonly', 'true');
$('#price').val(result['Price']);
$('#price').attr('readonly', 'true');
$('#choosepicture').attr('onclick', 'window.open(\'http://www.wishindex.com/picture.php?link=' + result['Link'] + '\')');
if (result['PictureLink'] != "")
{
$('#picturelink').val(result['PictureLink']);
$('#picturelink').attr('readonly', 'true');
}
else
$('#choosepicture').removeAttr('disabled');
$('#automatic').attr('value', 'true');
$('#currency').val(result['Currency']);
$('#currency').attr('readonly', 'true');
}
else
{
$('#automatic').attr('value', 'false');
$('#manual').html('Please enter details manually');
$('#choosepicture').removeAttr('disabled');
$('#choosepicture').attr('onclick', 'window.open(\'http://www.wishindex.com/picture.php?link=' + result['Link'] + '\')');
}
});
}
如果我啟用了警報,那么我看到調用的鏈接是正確的,JSON是有效的(通過firebug並手動調用該鏈接),並且執行了alert('inside')和alert('inside2'),因此可以達到此目的代碼段,但我的html元素未更新!
就像我說的那樣,在升級之前還不錯,但是也許我做錯了事,所以我將花費很多時間而無法找到問題,因此可以提供任何幫助,我們將不勝感激。
我的JSON響應;
[{"ID":"","MemberID":"24","Listid":"41","Itemname":"LEGO Star Wars 9489: Endor Rebel Trooper and Imperial Trooper","Description":null,"NumberDesired":null,"NumberPurchased":null,"Category":{"0":"TOYS_AND_GAMES"},"Link":"\"http:\/\/www.amazon.co.uk\/LEGO-Star-Wars-9489-Imperial\/dp\/B005KISGAI\/ref=pd_rhf_gw_shvl1\"","PictureLink":{"0":"http:\/\/ecx.images-amazon.com\/images\/I\/51fQnt%2BlppL._SL160_.jpg"},"Price":"9.89","Currency":"\u00a3","Priority":null,"SuggestedPurchaser":null,"ActualPurchaser":null,"PurchaseStatus":null,"Productid":"B005KISGAI","Site":"amazon.co.uk","DateAdded":null,"DatePurchased":null,"Domain":null,"Temp":["LEGO-Star-Wars-9489-Imperial","dp","B005KISGAI","ref=pd_rhf_gw_shvl1\""],"Error":"","Warning":null}]
您可以調用此方法以獲取JSON結果示例
可以在此處找到有效的演示(根據要求); http://www.wishindex.com/test_newitem.php?listid=41
去測試;
您的json是數組中的對象。 因此,您只能訪問以下數據: result[0]['Itemname']
或在訪問數據之前執行result = result[0]
。
因此它到達了“ inside2”,因為result['Itemname']
undefined
,即!= ""
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.