2012-07-30 22 views
-1

我有这个JS代码(使用jQuery表格插件):使用jQueryForm XHR为HTML

function doLogin(responseText, statusText, xhr, $form) { 
     var userbox = $(xhr).html(); 
     console.log(userbox); 
    } 

我想用userbox为HTML,这样我就可以切出(使用find())它的某些部分。我在这里做错了什么?


完整的登录代码:

$('#loginForm').submit(function() { 
    $(this).ajaxSubmit({ 
     'success': doLogin, 
     'error': showError 
    }); 

    function showError() { 
     $('#loginForm').append('<div class="errorMessage" style="display:none">Błędny login lub hasło.</div>'); 
     $('.errorMessage').fadeToggle(); 
    } 

    function doLogin(responseText, statusText, xhr, $form) { 
     var userbox = $(responseText).html(); 
     console.log(userbox); 
    } 

    return false; 
}) 

回答

0

好的,问题已经解决了,而且 - 几乎总是:) - 我的错误。

正如David Hedlund所说,responseText是关键。下面是更新后的代码,是任何人都需要它:

function doLogin(responseText, statusText, xhr, $form) { 
     var userbox = $(responseText).find('div#userBar'); 

     $('#loginForm').after(userbox) 
     console.log(userbox) 
    } 
0

xhr是您的XmlHttpRequest的参考。你打算写$(responseText).html()

+0

'$(responseText的)。html的()''返回时null''$(XHR)的.html()'返回代码(这是不符合工作' find()'虽然)。 – 2012-07-30 08:51:04

+0

如何调用'doLogin'?从服务器返回什么? '$(xhr).html()'产生什么代码?你在传递给'.find'的选择器是什么? – 2012-07-30 08:56:43

+0

我用完整的登录代码更新了我的问题。我错了,'$(responseText).html()'和'$(xhr).html()'返回null,'$(xhr)'单独返回整个页面源代码。 – 2012-07-30 08:58:52