简体   繁体   English

Jquery ajax Post 不工作?

[英]Jquery ajax Post is not working?

I have used ajax post without form.我使用了没有表格的ajax帖子。 When I click submit button it should get the values of other fields.当我单击提交按钮时,它应该获取其他字段的值。 Post request is not being sent and neither output is being shown.未发送发布请求,也未显示输出。 It is returning this error Unexpected identifier它正在返回此错误Unexpected identifier

Here are the input fields这是输入字段

<input type="text" name="name" id="name" class="center-block Ename" placeholder="Enter you name">
            <textarea class="center-block" name="message" id="message" rows="1" placeholder="Enter your message"></textarea>
            <input class="center-block sendBtn" type="submit" id="submit" name="submit" value="Submit">

This is the ajax request.这是ajax请求。

$(document).ready(function(){
        var interval = setInterval($('#submit').click(function(){
            var values = {
                'name': document.getElementById('name').value,
                'message': document.getElementById('message').value
            };
            $.ajax({
                type: "POST",
                url: "chat.php",
                data: values,
                success: function(data){
                    $("#chat").html(data);
                }
            });

        }),1000);

    });

It is sending request to this php page它正在向这个 php 页面发送请求

<?php
include 'db.php';


//Here post data is being assigned to variables
        $name = $_POST['name'];
        $message = $_POST['message'];
        $queryInsert = "INSERT INTO chat(`name`, `message`) VALUES('$name', '$message')";
        $queryInsertRun = mysqli_query($con, $queryInsert);
        if(!$queryInsertRun){
            echo mysqli_error($con);
        }



//Here is the output which should be shown

$query = "SELECT * FROM `chat` ORDER BY `name` AND `message` DESC ";
$queryRun = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($queryRun)){
$name = $row['name'];
$message = $row['message'];
?>
    <span class="name" style="font-weight: bold"><?php echo  $name?>:</span>
    <span class="message"><?php echo  $message.'<br>'?></span>
    <hr>
<?php
}
?>

I want to know that why is this not working.我想知道为什么这不起作用。

It sounds like your JSON Data is not valid.听起来您的 JSON 数据无效。

var data = JSON.stringify(values);
var request = $.ajax({
  url: "script.php",
  method: "POST",
  data: data,
  dataType: "html" // read about dataType
});

try this code试试这个代码

function ajaxcall()
{
console.log('i am called');
        var values = {
            'name': document.getElementById('name').value,
            'message': document.getElementById('message').value
        };

             $.ajax({
                type: "POST",
                url: "chat.php",
                data: values,
                success: function(data){
                    $("#chat").html(data);
                }
            });

}

$(document).ready(function(){
var id = setInterval(function() {
   ajaxcall();
}, 1000);
    });

http://jsfiddle.net/cod7ceho/116/ http://jsfiddle.net/cod7ceho/116/

You want to display data if you clicked submit button , use simple click function .如果您想在点击提交按钮时显示数据,请使用简单的点击功能。 Using setInterval can't help you for clicking submit .使用 setInterval 无法帮助您单击 submit 。 JS JS

$(document).ready(function(){
        $('#submit').click(function(){
            var values = {
                'name': document.getElementById('name').value,
                'message': document.getElementById('message').value
            };
            $.ajax({
                type: "POST",
                url: "chat.php",
                data: values,
                success: function(data){
                    $("#chat").html(data);
                }
            });

        });
      });

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

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