为什么这个jquery脚本不起作用


Why is this jquery script not working?

所以我有一个网站,允许用户输入链接,然后提取信息并将其放入他们的列表中。

现在一切正常,直到昨天我的网络托管合作伙伴进行了一些升级。 我知道mysql和php一直在升级,但不确定还有什么。

首先,我遇到了无法登录数据库的问题(必须删除并重新创建用户(。然后是 JSON 的 PHP 序列化问题(需要更改代码(。然后是 412 无效前提条件错误(需要托管合作伙伴设置的特殊规则(。现在最后,jquery脚本已经停止工作。 但我不知道为什么。 它以前奏效过,但也许那是运气(我在这方面没有那么经验(。

无论如何,发生的事情是

  1. 用户输入链接。
  2. jquery调用一个返回JSON的链接。
  3. 解析 JSON 并使用结果更新网页。
  4. 用户单击保存,数据被输入到数据库中。

使用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/ajax.php?link=%22http://www.amazon.co.uk/LEGO-Star-Wars-9489-Imperial/dp/B005KISGAI/ref=pd_rhf_gw_shvl1%22&listid=41&memberid=24

可以在此处找到工作演示(根据要求(;http://www.wishindex.com/test_newitem.php?listid=41

测试;

  1. 输入来自 amazon.co.uk 的产品链接,例如http://www.amazon.co.uk/LEGO-Star-Wars-9489-Imperial/dp/B005KISGAI/ref=pd_rhf_gw_shvl1进入链接字段
  2. 单击获取详细信息,其余字段应填充自动,但不是。

您的 json 是数组中的一个对象。 因此,您应该只能访问这样的数据: 在访问之前result[0]['Itemname']或执行result = result[0]

所以它到达"inside2",因为result['Itemname'] undefined这是!= ""