2014-01-31 26 views
0

我想在插件jquery.form.js(http://malsup.com/jquery/form/)中獲得純HTML作爲響應,但我無法得到它。所以,我的代碼看起來像這樣:jquery.form get plain HTML

print "$('#filter_form').ajaxForm({ 
      target:'#ajax1', 
    beforeSend: function() { 
     console.log('Image uploading started.'); 
     img_status.fadeOut(); 
     img_slider.width('0%'); 
     img_percent_text.html('0%'); 
    }, 
uploadProgress: function(event, position, total, percentComplete) { 
    console.log('Image upload update...'); 
    img_slider.width(percentComplete + '%'); 
    img_percent_text.html(percentComplete + '%') 
}, 

complete: function(responseText, statusText, xhr) { 
    img_slider.width('100%'); 
    img_percent_text.html('100%') 
    console.log('Image uploading finished!'); 
    img_preview.fadeOut(800); 
    alert(responseText); 
} 

    });"; 

print "});"; 
print '</script>'; 

出於某種原因,這種情況正在改變目標錯了,看看代碼...

if (empty($_POST)) { 
...... // I get this code as output 
} 
else{ 
print "this should be the output via ajax"; 
} 

所以問題是:

如何獲取完整函數中的純HTML(responseText返回對象)

如何解決我通過ajax發送的發佈參數?

+0

你如何在jsfiddle中測試表單,我沒有在那裏看到PHP選項 –

回答

0

您還可以將響應text/html的對象追加一些HTML實體,例如

$('#my_id').html(responseText); 

<div id='my_id'></div> 
+0

responseText返回[object Object],我已經試過了,並且div中的代碼保持不變。 –

+0

只是在這個對象中有另一個responseText,當我鍵入responseText.responseText它確實顯示輸出,但發佈查詢是錯誤的 –

1

請注意,empty功能,如果你在一個空數組傳遞返回FALSE()。

所以,如果你使一個POST沒有字段(params),那麼empty($_POST)返回FALSE

如果您想檢查請求是否爲POST類型,請檢查是否使用isset($_POST)方法或$_SERVER['REQUEST_METHOD'] === 'POST'設置了$ _POST變量。

+0

我使用它,因爲表單操作導致相同的頁面,其中窗體是,所以如果沒有帖子查詢,我打印表格,如果有,我只是處理提交的帖子參數。我會嘗試isset()... –

+0

好吧,只是想指出,你的if語句不會工作,當你做一個沒有參數的職位。但是,如果你期望始終有參數,那麼它不應該是一個問題。 – sjkm

+0

謝謝,我會用REQUEST_METHOD :) –

0

所以這裏是我的錯誤:

1.如何獲得作爲純HTML的響應?

responseText是包含responseText的對象。

Just use responseText.responseText\ 

我如何找到它: 我在瀏覽器控制檯中使用

console.log(responseText) 

輸出對象,我發現,源代碼是在responseText的。

2.用POST方法問題

我紅認爲,這個插件會從形式方法,以便有兩個修正 -

type: 'POST' 

$('...').ajaxForm({ 
與PARAM迫使它

或簡單地將表單方法設置爲如下發布:

<form action="example.com" method="post" ></form>