Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
Javascript 如何使用jquery post发送大数据而不使用重定向页面?_Javascript_Jquery_Json_Chat_Getjson - Fatal编程技术网

Javascript 如何使用jquery post发送大数据而不使用重定向页面?

Javascript 如何使用jquery post发送大数据而不使用重定向页面?,javascript,jquery,json,chat,getjson,Javascript,Jquery,Json,Chat,Getjson,我想知道如何在没有重定向页面的情况下使用jquery POST发送大数据? 我有一个创建移动聊天的项目,要在用户应用程序和服务器之间连接,我使用JSON。 这是jquery获取json脚本的样子,因为我们知道jsonGet不能处理大数据 注意:bigNumber有8000个字符 $.getJSON('process.php?input='+bigNumber, function(data) { $.each(data.Chats, function(i,output) {

我想知道如何在没有重定向页面的情况下使用jquery POST发送大数据? 我有一个创建移动聊天的项目,要在用户应用程序和服务器之间连接,我使用JSON。 这是jquery获取json脚本的样子,因为我们知道jsonGet不能处理大数据

注意:bigNumber有8000个字符

$.getJSON('process.php?input='+bigNumber, function(data) {
    $.each(data.Chats, function(i,output)
    {
        $("#data").append(output.chat);
    });
});
这就是使用getJSON发送大数字时得到的结果:414(请求URI太大)

因此,在发送bigNumber之后,我将从process.php获得一个响应,作为json数据并添加到html主体中

//--- 这是我的代码

html文件

<script src="jquery.min.js"></script>
<script>
$(function(){
    $("#senddata").click(function() {
        $.ajax({
            dataType: "json",
            url: "process.php",
            data: { input:$("#bigNumber").val() },
            success: function(data) {
                $.each(data.Chats, function(i,output)
                {
                    $("#data").append(output.key);
                });
            },
            method: "POST"
        });
    });
});
</script>
<div id="data"></div>
<input type="text" id="bigNumber"/>
<button id="senddata">Send</button>
当我填写文本框并按下“发送”按钮时,什么也没发生。 怎么了



刚刚发现问题所在,是json格式的错误,显示在process.php上。您需要使用POST请求。由于
getJSON()
只是
ajax()
的一个门面,因此转换起来非常容易:

header('Content-type: application/json');

$key = $_POST["input"];

$simpan_array = array();

$chat = array();
$chat["key"] = $key;

array_push($simpan_array, $chat);

echo json_encode(array("Chats" => $simpan_array));
$.ajax({
  dataType: "json",
  url: "process.php",
  data: { input:bigNumber },
  success: function(data) {
    $.each(data.Chats, function(i,output)
    {
        $("#data").append(output.chat);
    });
  },
  method: "post"
});

使用
$.post

$.post('process.php',{input: bigNumber}, function(data) {
  $.each(data.Chats, function(i,output)
  {
    $("#data").append(output.chat);
  });
},'JSON');

为什么您不能尝试将WebSocket用于聊天应用程序?请参阅本页
或者

这通常意味着您有一个未闭合的括号或括号。请确保您所有的代码都正确关闭。先生,我已经尝试了您的代码,但什么也没发生。你介意在上面看看我的新代码吗???当然什么也没发生。单击处理程序在哪里。现在,您所要做的就是在页面加载时发出AJAX调用。但是您需要将AJAX调用嵌套在click处理程序中,但仍然没有发生任何事情:(lol我忘了在jquery代码的末尾加上“}”);:D谢谢bipeni我已经尝试了您的代码,但是没有发生任何事情。你介意在上面看看我的新代码吗???