jQuery POST之后调用方法 [英] Call a method after jQuery POST
问题描述
当我使用.click事件时,可以调用该方法.
工作
When I use .click event I can call the method.
WORKING
$("#signin").live('click',function(){
jsTouch.loadPage('pages/tabs.html', { transition: 'pop-in' });
});
当我使用jQuery.post时,无法加载该方法.
失败
When I use the jQuery.post it fails to load the method.
FAILED
$("#signin").live('click',function(){
$.post("http://localhost/myAPI/index.php/api/requestlogin", { username: "admin", password: "test" },
function(data) {
if(data.response == 'success')
{
jsTouch.loadPage('pages/tabs.html', { transition: 'pop-in' });
}else{
alert(data.response);
}
});
});
控制台错误 TypeError:无法调用未定义的方法"loadPage"
CONSOLE ERROR TypeError: Cannot call method 'loadPage' of undefined
这是jstouch库. https://github.com/vitmalina/Web-2.0 -Touch/blob/master/includes/jsTouch.js
Here is the jstouch library. https://github.com/vitmalina/Web-2.0-Touch/blob/master/includes/jsTouch.js
推荐答案
首先,对.live()
进行折旧.改用.on()
: http://api.jquery.com/on/
Firstly, .live()
is depreciated. Use .on()
instead: http://api.jquery.com/on/
好的,所以我用我使用的AJAX函数重新编写了您的代码. $ .POST是$ .ajax的简写,但还是可以尝试一下:)
Okay so I've re-written your code with the AJAX function that I use. $.POST is, yes, short-hand for $.ajax, but give this a try anyway :)
$("body").on('click', '#signin', function(){
$.ajax({
url: "http://localhost/myAPI/index.php/api/requestlogin",
type: 'POST',
data: {username: "admin", password: "test"},
success: function(response)
{
jsTouch.loadPage('pages/tabs.html', { transition: 'pop-in' });
},
error: function(response)
{
alert('error');
}
});
});
一旦我们知道您使用的代码肯定可以正常工作,但仍然没有任何反应,则需要按以下顺序检查以下内容:
Once we know that you're using code that should definitely be working, and still nothing happens, you need to check the following, in this order:
- 该点击确实正在注册,因此在该点击之后立即发出警报.
- 该POST确实正在执行中,因此请在成功功能中添加一个警报.
- 如果仍然没有任何反应,则是您的
jsTouch.loadPage()
无法正常工作.
- That the click is indeed registering, so put an alert just after the click.
- That the POST is indeed going through, so put an alert in the success function.
- If still nothing happens, it's your
jsTouch.loadPage()
that's not working.
这篇关于jQuery POST之后调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!