【问题标题】:Jquery function stopped workingJquery函数停止工作
【发布时间】:2012-02-07 23:37:30
【问题描述】:

我使用这个脚本已经有一段时间了,突然它停止工作并返回错误消息。我找不到任何代码错误,并且 php 页面回显了有效的 json。如果有人发现此代码有错误或有问题,请告诉我。我在其他地方使用相同的脚本就好了。

转到: http://ab-mobile-apps.com/app/grotto/index.html 然后点击随机饮料看直播。点击错误信息会再次调用该函数。

先谢谢了。

function loadData() {                              
    var output = $('#output');
    var drinkImageOutput = $('#drinkImage');
    var drinkIngredientOutput = $('#drinkIngredient');
    var drinkNameOutput = $('#drinkName');

    output.text('');

    $.ajax({

        url: 'http://ab-mobile-apps.com/grototest/index.php',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 10000,
        success: function(data, status){
        $.each(data, function(i,item){
        var landmark = 
            '<div id="drinkImage"><img src="' + drinkImg + '" width="15%" /></div>' + 
            '<div id="drinkName">' + drinkName + '</div>' +
            '<div id="dringIngredient">' + dringIngredient + '</div>';


        output.append(landmark);
        });
        },
        error: function(){
        output.text('There was an error loading the data.');
        }
    });            
}

【问题讨论】:

  • 使用 Chrome 中的调试器来单步调试代码...
  • 更新了您的评论 Kris 并进一步详细说明,在 Chrome 开发工具中(按 F12 打开),转到“网络”选项卡...在那里您将看到 AJAX 请求并找出什么是被返回,从那里你应该能够调试你的脚本
  • 感谢您的回复。在发布之前,我使用 chrome 多次调试脚本,并且似乎跳过了 ajax 调用。你用我发布的网址试过了吗?请做并提供反馈。

标签: php jquery json httpwebrequest


【解决方案1】:

您的请求返回 JSON 而不是 JSONP。 试试:

$.ajax({
    url: 'http://ab-mobile-apps.com/grototest/index.php',
    dataType: 'json',
    timeout: 10000,
    success: function(data, status){
    $.each(data, function(i,item){
    var landmark = 
        '<div id="drinkImage"><img src="' + item.url + '" width="15%" /></div>' + 
        '<div id="drinkName">' + item.sname + '</div>' +
        '<div id="dringIngredient">' + item.ingredients + '</div>';


    output.append(landmark);
    });
    },
    error: function(){
    output.text('There was an error loading the data.');
    }
}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多