jquery ajax have error

jquery ajax have error

本文关键字:error have ajax jquery      更新时间:2023-09-26
var send = {ff_ID:"",ff_k:""};
send.ff_ID  = $(this).attr("id");
send.ff_k = $(this).find("span").text();
$.ajax({
   type: "POST",
   url: "/funfact_ajax",
   cache:true,
   dataType:"json",
   data:send,
   success: function(data){
     if(data.success == true){
         $(this).text(eval(send.ff_k)+1);
      }else{
        alert(data.msg);
    }
    }
});

问题是这一行"$(this).text(eval(send.ff_k)+1);"

如果我在ajax之外使用它,这是没有问题的。

(美元)。text("该死的");

有相同的错误…

这里是错误:

[Error] TypeError: undefined不是一个对象(求值' . createdocumentfragment ')Db (jquery-1.11.1.min.js,第3行)buildFragment (jquery-1.11.1.min.js,第3行)domManip (jquery-1.11.1.min.js,第3行)追加(jquery-1.11.1.min.js,第3行)(匿名函数)(jquery-1.11.1.min.js,第3行)访问(jquery-1.11.1.min.js,第3行)文本(jquery-1.11.1.min.js,第3行)成功(funfact,第192行)J (jquery-1.11.1.min.js,第2行)fireWith (jquery-1.11.1.min.js,第2行)X (jquery-1.11.1.min.js第4行)B (jquery-1.11.1.min.js, line 4)

我不知道是什么问题,需要帮助,谢谢!

错误可能是由于this是普通的Object而不是Element

每个function都有自己的this值,在调用时确定。并且,在success回调中,this通常会引用请求的设置。

$.ajax({
    // ...
    success: function () {
        console.log(this.type, this.url); // "POST" "/funfact_ajax"
    }
});

jQuery.ajax()包含一个context选项,用于指定要使用的不同值,因此它也可以引用那里的Element:

$.ajax({
    // ...
    context: this,
    success: function () {
        $(this).text(eval(send.ff_k)+1);
    }
});