所以我有一个网站,允许用户输入链接,然后提取信息并将其放入用户的列表中。
现在一切都工作正常,直到昨天我的网络托管合作伙伴完成了一些升级。我知道 mysql 和 php 已经升级,但不知道还有什么。
首先,我遇到了无法登录数据库的问题(必须删除并重新创建用户)。 然后是 PHP 序列化为 JSON 的问题(需要更改代码)。 然后是 412 无效前提条件错误(需要托管合作伙伴设置特殊规则)。 现在最后一个 jquery 脚本已停止工作。但我不知道为什么。以前确实有效,但也许那是运气(我在这方面没有那么丰富的经验)。
无论如何,发生的事情是
- 用户输入链接。
- jquery 调用返回 JSON 的链接。
- 解析 JSON 并使用结果更新网页。
- 用户点击“保存”,数据就会输入到数据库中。
使用 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 并手动调用链接)并且警报('inside')和警报('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
测试;
- 输入来自 amazon.co.uk 的产品链接,例如 http://www.amazon.co.uk/LEGO-Star-Wars-9489-Imperial/dp/B005KISGAI/ref=pd_rhf_gw_shvl1 进入链接字段
- 点击“获取详细信息”,然后应填充其余字段 自动,但他们不是。
最佳答案
你的 json 是数组中的一个对象。因此您应该只能访问如下数据: result[0]['Itemname']
或在访问之前执行 result = result[0]
。
所以它到达'inside2',因为result['Itemname']
是undefined
,即!= ""
关于php - 为什么这个 jquery 脚本不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11791511/