2015-09-07 90 views
1

我有這樣一個JSON對象(分配給var card;):使用jQuery AJAX以JSON對象傳遞給PHP文件

{ 
    card_no: "1", 
    card_name: "wwwgdefonru", 
    img_id: 1, 
    img_thumb: "albums/070915_E239/thumbs/001_wwwgdefonru.jpg", 
    img_hires: "albums/070915_E239/thumbs_hires/001_wwwgdefonru.jpg" 
} 

我想這個使用AJAX傳遞給PHP支持文件。

這裏是我的AJAX調用:

jQuery.ajax({ 
    method: 'POST', 
    url: ajax_file, 
    type: 'JSON', 
    data: card, 
    dataType: 'html', 
    cache: false, 
    success: function(html){ 
     console.log('SUCCESSO'); 
     jQuery('#debug').html(html);  
    }, 
    complete:function(){ 
     console.log('COMPLETE'); 
    } 
}); 

在我的PHP文件(只用於調試)I輸出傳遞的數據,像這樣:

echo '<pre>'; 
    print_r($_POST); 
echo '</pre>'; 

的AJAX調用成功完成。但輸出爲空:

Array 
(
) 

我在哪裏出錯了?

SOLUTION

我工作的一個項目與舊版本的jQuery(不要問...)。從查看文檔(http://api.jquery.com/jquery.ajax/)我可以看到使用method: 'POST'設置方法不起作用,因爲它不受我使用的JQ版本的支持。切換到type: 'POST'並擺脫type: 'JSON'修復它。

我不會發現這個,我沒有看過網絡標籤,看到請求方法是GET即使我已經定義它爲POST

+0

替換'方法:'用'type:' – Rasclatt

+0

@mccannf - 沒有區別。我得到了相同的結果 – user3065931

+0

@Rasclatt試圖用mccannfs的建議和沒有。同樣的結果。 – user3065931

回答

-1

post方法的方法是用這種方式是那麼容易讓你

$.post("your_php_file_url.php", 
    { 
     card_no: "1", 
     card_name: "wwwgdefonru", 
     img_id: 1, 
     img_thumb: "albums/070915_E239/thumbs/001_wwwgdefonru.jpg", 
     img_hires: "albums/070915_E239/thumbs_hires/001_wwwgdefonru.jpg" 
    }, 
    function(data, status){ 
     alert("Response Message" + data + "\nResponse Status: " + status); 
    }); 

在你的PHP的網站,你閱讀使用

<?php 
    echo $_POST['card_no']; 
?> 

這樣你可以得到其他值的價值和做你的作品!

+2

從頭開始重新開始*可能會解決問題,除非您可以識別現有代碼的實際問題,否則難以確定。 – Quentin