我有以下AJAX:
$.ajax({
url: url,
data: {"url" : hangOutUrl, "participants" : params},
dataType: 'jsonp',
success: function(){
console.log("Connected");
},
error: function(xhr, textStatus, error) {
console.log("Impossible to connect");
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
这是我控制器上的代码
def hangout_started
#Get information from hangout using ajax and widget
url = params[:url]
participants = params[:participants].split(/-/)
#Find users
caller1 = Kid.where(username: participants[0]).first
caller2 = Kid.where(username: participants[1]).first
#Set all users to busy
caller1.set_busy_status!
#Create REDIS row with user and URL
REDIS.sadd "hangout:#{caller2.id.to_s}", url
REDIS.expire "hangout:#{caller2.id.to_s}", 60
respond_to do |format|
format.json { head :ok }
end
end
这项工作非常完美,除了我总是在我的控制台日志中“无法连接” . 为什么我总是进入我的错误功能?
浏览器控制台的错误功能和发布错误:
消息:“jQuery211019731531548313797_1401196032110未被调用”堆栈:“错误:jQuery211019731531548313797_1401196032110未在Function.n.extend.error中调用(https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery . min.js:2:1821)↵at h.jsonp.b.dataTypes . (匿名函数).b.converters.script json(https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/ jquery.min.js:4:16293)↵at vc(https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:4:7397)↵xat x(https: https://ajax.googleapis.com/上的//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:4:10802)↵ ajax / libs / jquery / 2.1.1 / jquery.min.js:4:15583)↵在HTMLScriptElement.n.event.dispatch(https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/ jquery.min.js:3:6404)↵在HTMLScriptElement.r.handle(https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:3:3179)“
1 回答
Ajax
关于
$.ajax
你需要知道的事情 - 当你使用error
回调时,它是由你的ajax命中 actual error 引起的 . 我曾经认为如果你的应用程序返回错误 - 这是错误的:这意味着如果错误出现在您的控制器中,我相信它会从您的控制器返回
success
回调Fix
对你来说,我会推荐这个:
确定您的
ajax
正在点击url
(您的代码中没有详细说明url
)请确保您在javascript
application
js中包含jquery
使用一种方法从
ajax
捕获返回的error
尝试使用
json
您可能希望将
ajax
更改为this:这将允许您正确捕获错误响应,使您能够正确显示错误 . 你可以通过这样做来做到这一点:
如果你给我一些工作,我会更好地帮助你!