jQuery的AJAX JSON错误 [英] jQuery AJAX json error

查看:97
本文介绍了jQuery的AJAX JSON错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的JSON对象。

Here is my JSON object.

{
        "verbs" : [
    	"process",
    	"refine",
    	"define"
        ],
        "adjectives" : [
    	"process",
    	"audio",
    	"language"
        ],
        "subjects" : [
    	"process",
    	"development",
    	"technique"    
            ]
}

下面是我尝试通过jQuery的AJAX方法来访问和处理数据。

Here is my attempt to access and process the data via the jQuery AJAX method.

jQuery.ajax({
    type : "POST",
    dataType : "json",
    url : "js/tsbtw-object.js",
    success : function(data, statusText){

    	var verbArray = data.verbs;

    	for(var i = 0; i<verbArray.length; i++){

    		var verbTime = Math.floor(Math.random()*1000);

    		jQuery("#verb-content").fadeOut(verbTime, function(){
    			(this).text(verbArray[i]).fadeIn(verbTime);
    		});
    	}

    },
    error: function (xhr, ajaxOptions, thrownError){
    	alert(xhr.statusText);
    	alert(thrownError);
    }   
});

我收到了Firebug的控制台两个错误。

I am receiving two errors in the FireBug console.

标签无效 动词:[\ñ

invalid label "verbs" : [\n

this.text不是函数 (本)的.text(verbArray [J]。)淡入(verbTime);。\ñ

this.text is not a function (this).text(verbArray[j]).fadeIn(verbTime);\n

我涨较晚试图解了这一点,并认为我会踢出来了社区的洞察力。

I was up fairly late trying to puzzle this out, and thought I would kick it out the community for insight.

谢谢!

推荐答案

尝试 $(本)的.text 而不是(本)的.text 。另外请注意,因为你是在Ajax回调点为Ajax请求的选项,因此文本功能可能不被限定。相反,你可以尝试使用:

Try $(this).text instead of (this).text. Also note that as you are in an ajax callback this points to the options for the ajax request, so the text function might not be defined. Instead you could try with:

var _this = this;
jQuery.ajax({
    type : "POST",
    dataType : "json",
    url : "js/tsbtw-object.js",
    success : function(data, statusText){
        var verbArray = data.verbs;
        for(var i = 0; i<verbArray.length; i++){
            var verbTime = Math.floor(Math.random()*1000);
            jQuery("#verb-content").fadeOut(verbTime, function(){
                    $(_this).text(verbArray[i]).fadeIn(verbTime);
            });
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.statusText);
        alert(thrownError);
    }   
});

这篇关于jQuery的AJAX JSON错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆