jQuery的AJAX JSON错误错误、jQuery、AJAX、JSON

2023-09-10 19:04:53 作者:左手的旁边是你的右手丶

下面是我的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);
    }   
});