1

我想知道如何在没有重定向页面的情况下使用 jquery POST 发送大数据?我有一个项目来创建移动聊天,并在用户应用程序和服务器之间进行连接,我使用 JSON。这是 jquery get 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>

这是process.php

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));

当我填写文本框并按下“发送”按钮时,什么也没有发生。怎么了?


刚刚发现是什么问题,是json格式的错误,显示在process.php上

4

3 回答 3

2

您需要改用 POST 请求。由于getJSON()只是 的门面ajax(),因此很容易转换:

$.ajax({
  dataType: "json",
  url: "process.php",
  data: { input:bigNumber },
  success: function(data) {
    $.each(data.Chats, function(i,output)
    {
        $("#data").append(output.chat);
    });
  },
  method: "post"
});
于 2013-04-17T04:25:51.473 回答
1

利用$.post

$.post('process.php',{input: bigNumber}, function(data) {
  $.each(data.Chats, function(i,output)
  {
    $("#data").append(output.chat);
  });
},'JSON');
于 2013-04-17T04:27:17.717 回答
0

为什么你不能尝试将 websockets 用于聊天应用程序请参阅此页面 Web Socket ServerHTML5 WebSocket 简介

于 2013-04-17T05:30:09.867 回答