通过POST或GET发送大量数据

[英]Sending lots of data through POST or GET


I am trying to send an XML file from a textfield in my html, via ajax to a PHP file. This is the almighty PHP file:

我试图从我的html中的文本字段发送XML文件,通过ajax发送到PHP文件。这是万能的PHP文件:

<?php 
    $data = urldecode($_POST["xml"]);

    echo $data;
?>

Data is sent to this file as such:

数据如下发送到此文件:

$("#btn_save").click(function() {
    var data = escape($("#textfield").text());
    alert(data);
    $.ajax({
        url:        "validate.php",
        method:     "POST",
        data:       "xml=" + data,
        complete:   function(e) { alert(e.responseText); }
    });
});

Now, as long as I don't sent more than a few lines of code, it works as it should. When I paste in a 60 line XML file however, validate.php returns

现在,只要我没有发送多行代码,它就可以正常工作。但是,当我粘贴60行XML文件时,validate.php会返回

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /xml_stylist/form/validate.php
on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at dahwan.info Port 80</address>
</body></html>

What am I doing wrong?

我究竟做错了什么?

Thanks

谢谢

6 个解决方案

#1


5  

Change

更改

method: "POST"

to

type: "POST"

that may do the trick.

这可能会成功。

#2


4  

BenSho is correct, the argument is called type. In addition:

BenSho是正确的,这个论点叫做类型。此外:

$("#textfield").text()

I'm guessing that's a <textarea>. You shouldn't use text() or html() to read content from an input field, it doesn't do what you think. Use val().

我猜这是一个

var data = escape($("#textfield").text());

Don't ever use escape(). It is a weirdo JavaScript-specific function that looks like URL-encoding but isn't. If you use it for URL-encoding you will corrupt plus signs and all non-ASCII characters.

不要使用escape()。它是一个怪异的JavaScript特定函数,看起来像URL编码但不是。如果您将其用于URL编码,则会损坏加号和所有非ASCII字符。

The correct JavaScript function for URL-encoding is encodeURIComponent(). However, since you are using jQuery, much better to let it work out URL-encoding for you by passing an object in:

URL编码的正确JavaScript函数是encodeURIComponent()。但是,既然你正在使用jQuery,那么通过传递一个对象,让它为你编写URL编码要好得多:

data: {xml: $("#textfield").text()},

Finally:

最后:

$data = urldecode($_POST["xml"]);

You don't have to (and shouldn't) URL-decode anything manually. PHP URL-decodes parameters in a request body into raw strings for you.

您不必(也不应该)手动对任何内容进行URL解码。 PHP URL将请求正文中的参数解码为原始字符串。

#3


1  

Most browsers have a practical maximum of 2,083 characters in the url; there is no limit for a POST request. A GET request will pass the parameters in the url whereas a post does not. It all depends on how much you're actually sending to determine which you should use or if you're sending sensitive data (use POST).

大多数浏览器的网址实际最多为2,083个字符; POST请求没有限制。 GET请求将传递URL中的参数,而帖子则不传递。这一切都取决于您实际发送的数量以确定您应该使用哪些内容,或者您​​是否正在发送敏感数据(使用POST)。

#4


0  

Setting the data option on ajax calls means jquery will add these to the query string in a GET request. Most browsers have a limit on the length of a get request. If your xml data is too big, you should switch to POST.

在ajax调用上设置数据选项意味着jquery会将这些选项添加到GET请求中的查询字符串中。大多数浏览器都限制了get请求的长度。如果您的xml数据太大,则应切换到POST。

#5


0  

Optimize your php.ini

优化你的php.ini

  • post_max_size you may have to set your memory_limit to higher value..
  • post_max_size你可能需要将memory_limit设置为更高的值..
  • depends on the memory usage of your
    script also
  • 还取决于脚本的内存使用情况
  • max_execution_time could be a problem
  • max_execution_time可能是个问题

#6


0  

try this:

尝试这个:

$("#btn_save").click(function() {
    var data = $("#textfield").text();
    $.ajax({
        url:        "validate.php",
        type:       "POST",
        data:       {"xml": data},
        complete:   function(e) { alert(e.responseText); }
    });
});
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2010/09/15/4cb159cd05c03c333d05e1a7025b532d.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告