【问题标题】:How to send large data using jquery post without redirect page?如何在没有重定向页面的情况下使用 jquery post 发送大数据?
【发布时间】:2013-04-17 04:21:49
【问题描述】:

我想知道如何在没有重定向页面的情况下使用 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 (Request-URI Too Large)

所以,在我发送 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上

【问题讨论】:

    标签: javascript jquery json chat getjson


    【解决方案1】:

    您需要改用 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"
    });
    

    【讨论】:

    • 这通常意味着你有一个未闭合的括号或括号。确保您的所有代码都正确关闭。
    • 先生,我已经尝试过您的代码,但没有任何反应。你介意在上面看看我的新代码吗???
    • 当然什么也没发生。您的点击处理程序在哪里。现在你所做的就是在页面加载时触发 AJAX 调用。但是您需要将 AJAX 调用嵌套在单击处理程序中。
    • 仍然没有任何反应:(
    【解决方案2】:

    使用$.post

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

    【讨论】:

    • 大声笑我忘了放“});”在 jquery 代码的末尾:D 感谢 bipen
    • 我已经尝试过您的代码,但没有任何反应。你介意在上面看看我的新代码吗???
    【解决方案3】:

    为什么你不能尝试使用 websockets 进行聊天应用程序参考这个页面 Web Socket ServerIntroduction to HTML5 WebSocket

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2012-11-05
      相关资源
      最近更新 更多