簡體   English   中英

AJAX jQuery不顯示返回的數據

[英]AJAX jQuery doesn't display returned data

我有一個腳本來提取數據並將其加載到div中,如果我不使用任何表單值並且不使用type =“ POST”,則代碼可以正常工作並顯示用戶ID列表,但是它不會當我使用$ _POST值時,t工作(沒有結果,也沒有控制台錯誤消息):

HTML:

 <form name='newUser' id="searchForm" method="POST">
<input type="text" name="look_for" id="look_for">
<input type="text" name="lookage_from" id="age_from">
<input type="text" name="age_to" id="age_to">
</form>

fetchusers.php

$look_for = $_POST['look_for'];
$age_from = $_POST['age_from'];
$age_to = $_POST['age_to'];
$array = getProfilePicsList($look_for,$age_from,$age_to); //a function that select * from ...
echo json_encode($array);

jQuery代碼:

$("#searchForm").submit(function(event)
{
    $.ajax({
        type: 'POST',
        url: 'models/fetchUsers.php', //the script to call to get data
        dataType: 'json',             //data format
        success: function(data)       //on recieve of reply
        {
            $.each($(data), function(key, value) {
                $('#itemContainer').append(value.user_id);
            });
        }
    });
    return false;
});

在您的Ajax請求中,您缺少data 只需添加:

data: $("#searchForm").serialize(),

例:

$.ajax({
    type: 'POST',
    url: 'models/fetchUsers.php', //the script to call to get data
    dataType: 'json',             //data format
    data: $("#searchForm").serialize(),
    success: function(data)       //on recieve of reply
    {
        $.each($(data), function(key, value) {
            $('#itemContainer').append(value.user_id);
        });
    }
});

如果不使用data參數,則無法獲取$_POST數組中的值。

一些參考: https : //api.jquery.com/jquery.post/ http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM