如何在jquery ajax成功回调函数中传递上下文
Posted
技术标签:
【中文标题】如何在jquery ajax成功回调函数中传递上下文【英文标题】:How to pass context in jquery ajax success callback function 【发布时间】:2011-04-21 06:47:21 【问题描述】:var Box = function()
this.parm = name:"rajakvk",year:2010;
Box.prototype.jspCall = function()
$.ajax(
type: "post",
url: "some url",
success: this.exeSuccess,
error: this.exeError,
complete: this.exeComplete
);
this.exeSuccess = function()
alert(this.parm.name);
我没有在 exeSuccess 方法中获取 Box 对象。如何在 exeSuccess 方法中传递 Box 对象?
【问题讨论】:
【参考方案1】:使用context
option,像这样:
$.ajax(
context: this,
type: "post",
url: "some url",
success: this.exeSuccess,
error: this.exeError,
complete: this.exeComplete
);
上下文选项决定了调用回调的上下文......因此它决定了this
在该函数中引用的内容。
【讨论】:
非常抱歉。过目不忘的 jQuery 文档。这里明确提到了api.jquery.com/jQuery.ajax 可能有明确提及,但不清楚如何使用。尼克的例子很有帮助。这篇文章更详细:***.com/questions/5097191/ajax-context-option以上是关于如何在jquery ajax成功回调函数中传递上下文的主要内容,如果未能解决你的问题,请参考以下文章
用jquery中的ajax进行前后台,后台的方法正常执行,在前台接收不到后台传递的数据