简体   繁体   English

JSON从PHP脚本返回值的ajax jquery错误

[英]ajax jquery errors on json return value from php script

I'm having problems with this ajax/jquery programming. 我在使用此ajax / jquery编程时遇到问题。 I've tried many different things but nothing has worked. 我尝试了许多不同的方法,但没有任何效果。

Ajax posts selItem to ajaxsql.php, this works! Ajax将selItem发布到ajaxsql.php,这有效! The sql query in ajaxsql.php works, cause it outputs this if i call the script directly in the browser: [{"forumname":"SDE forum","user":"michael","txt":"Jeg hedder Michael!"}] ajaxsql.php中的sql查询有效,如果我直接在浏览器中调用脚本,则会导致此输出: [{"forumname":"SDE forum","user":"michael","txt":"Jeg hedder Michael!"}]

The problem is that the ajax function shows an alert box with Error[object Object] 问题是,ajax函数显示带有错误[对象对象]的警报框

forum.php script: forum.php脚本:

<script type="text/javascript">

function ForumChat(selItem) {


 $.ajax({
        type: "POST",
        url: 'ajaxsql.php',
        data: { selectedItem : selItem.value },
        dataType: "json",
        success: function(data) {
            alert(data);
            $('#txtarea').html(data);
        },

        error: function(data) {
            alert('Error' + data);
        }
    });


}
</script>

ajaxsql.php script: ajaxsql.php脚本:

<?php
if(!isset($_SESSION))
{
session_start();
}
include('class.php');

//$sel = $_POST['selectedItem'];
$sel = "SDE forum";


$sql = " SELECT * FROM forum WHERE user = '".$_SESSION['currentuser']."' AND forumname = '".$sel."' ";


    $result = mysqli_query($_SESSION['con'], $sql);

        while($row = mysqli_fetch_array($result)) 
        {       
            $forumname = $row['forumname'];
            $user = $row['user'];
            $txt = $row['text'];

            $return[] = array("forumname" =>$forumname, "user" =>$user, "txt" =>$txt);

        }

        echo json_encode($return);

?>

because ajaxsql.php returns object .. 因为ajaxsql.php返回对象..

what you can do in your ajax is 你可以在ajax中做的是

success: function(response) {
     $('#txtarea').html('');
     $.each(response.data, function(){
         console.log(this);
         $('#txtarea').append(data);
     });
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM