Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个脚本中发布数据并使用JSON和Javascript进行检索_Javascript_Jquery_Json_Html Framework 7 - Fatal编程技术网

在一个脚本中发布数据并使用JSON和Javascript进行检索

在一个脚本中发布数据并使用JSON和Javascript进行检索,javascript,jquery,json,html-framework-7,Javascript,Jquery,Json,Html Framework 7,如何结合脚本1和脚本2来实现发送数据和一个脚本的目标 这样做的目的是在任何时候发送邮件时都能获得新的内容。我使用这个框架7。这两个脚本在其发布或检索数据的角色中已经运行良好 这是从后端获取/获取数据的脚本 脚本1 <script type="text/javascript"> $(document).ready(function() { var url = "http://localhost/integration/json.php"; $.g

如何结合脚本1和脚本2来实现发送数据和一个脚本的目标

这样做的目的是在任何时候发送邮件时都能获得新的内容。我使用这个框架7。这两个脚本在其发布或检索数据的角色中已经运行良好

这是从后端获取/获取数据的脚本

脚本1

<script type="text/javascript">
    $(document).ready(function() {
        var url = "http://localhost/integration/json.php";
        $.getJSON(url, function(result) {
            console.log(result);
            $.each(result, function(i, field) {
                var comment = field.comment;
                var user = field.user;
                var post_time = field.post_time;
                $("#listview").append("<tr class='even gradeA' width = '30px'><td>"+comment+"</td><td>"+user+"-"+post_time+"</td></tr>");
            });
        });
    });
</script>

$(文档).ready(函数(){
变量url=”http://localhost/integration/json.php";
$.getJSON(url、函数(结果){
控制台日志(结果);
$.each(结果、函数(i、字段){
var comment=field.comment;
var user=field.user;
var post_time=field.post_time;
$(“#列表视图”).append(“+comment+”+user+”-“+post#u time+”);
});
});
});
脚本2
脚本2的作用是将数据发布到服务器

<script type="text/javascript">
    $(document).ready(function() {
            $("#insert").click(function() {
                var comment = $("#comment").val();
                var user = $("#user").val();
                var ip = $("#ip").val();
                var dataString = "comment=" + comment + "&user=" + user + "&ip=" + ip + "&insert=";
                if ($.trim(comment).length > 0 & $.trim(user).length > 0 & $.trim(ip).length > 0) {
                $.ajax({
                    type: "POST",
                    url: "http://localhost/integration/insert.php",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                    beforeSend: function() {
                        $("#insert").val('Connecting...');
                    },
                    success: function(data) {
                        if (data == "success") {
                            alert("Successfully submitted");
                            $("#insert").val('submit');

                        } else if (data == "error") {
                            alert("error");
                        }
                    }
                });
            }
                return false;
        });
    });
</script>

$(文档).ready(函数(){
$(“#插入”)。单击(函数(){
var comment=$(“#comment”).val();
var user=$(“#user”).val();
var ip=$(“#ip”).val();
var dataString=“comment=“+comment+”&user=“+user+”&ip=“+ip+”&insert=“”;
if($.trim(comment).length>0&$.trim(user).length>0&$.trim(ip).length>0){
$.ajax({
类型:“POST”,
url:“http://localhost/integration/insert.php",
数据:dataString,
跨域:是的,
cache:false,
beforeSend:function(){
$(“#插入”).val('连接…');
},
成功:功能(数据){
如果(数据=“成功”){
警报(“成功提交”);
$(“#插入“).val(‘提交’);
}else if(数据==“错误”){
警报(“错误”);
}
}
});
}
返回false;
});
});
两个脚本都独立工作。

请尝试以下代码:

这就是我所做的。我已经将其转换为一个函数,一旦插入代码完成,您就可以调用该函数,并且在刷新页面时也可以调用该函数

函数getUpdateData(){ 变量url=”http://localhost/integration/json.php"; $.getJSON(url、函数(结果){ 控制台日志(结果); $.each(结果、函数(i、字段){ var comment=field.comment; var user=field.user; var post_time=field.post_time; $(“#列表视图”).append(“+comment+”+user+”-“+post#u time+”); }); }); } $(文档).ready(函数(){ getUpdateData(); $(“#插入”)。在('单击',函数()上{ var comment=$(“#comment”).val(); var user=$(“#user”).val(); var ip=$(“#ip”).val(); var dataString=“comment=“+comment+”&user=“+user+”&ip=“+ip+”&insert=“”; if($.trim(comment).length>0&$.trim(user).length>0&$.trim(ip).length>0){ $.ajax({ 类型:“POST”, url:“http://localhost/integration/insert.php", 数据:dataString, 跨域:是的, cache:false, beforeSend:function(){ $(“#插入”).val('连接…'); }, 成功:功能(数据){ 如果(数据=“成功”){ 警报(“成功提交”); $(“#插入“).val(‘提交’); getUpdateData(); }else if(数据==“错误”){ 警报(“错误”); } } }); } 返回false; });
});将两个代码放在一个
$(文档)中。准备好(函数(){..})
尝试了它。在success中提交数据似乎有一个问题:你说这两个都工作得很好?它们独立工作。我不太清楚为什么提交保持“连接”,因为它没有退出数据提交success:function(data){if(data==“success”){alert(“Successfully submitted”);$(“#insert”).val('submit');getupdatedata();}else if(data==“error”){alert(“error”);}您在控制台中遇到了什么错误?函数是否被调用?没有错误,但提交按钮保持“连接”。控制台显示[jquery.js:4 XHR finished loading:POST"好的,有点不清楚。首先,由于所有内容都在本地,您不需要给出
http://
,只需在AJAX中使用
integration/insert.php
。您可以从AJAX函数中删除
crossDomain:true,cache:false,
。其次,在php文件中,执行一个简单的
,看看是否有效。将
console.log()
在JS中的不同点,如单击、成功、错误,以了解它在哪里中断。