开发者

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:

<?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

<!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>
&l开发者_如何学Pythont;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


Change

method: "POST"

to

type: "POST"

that may do the trick.


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

$("#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.

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:

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.


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


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.


Optimize your php.ini

  • post_max_size you may have to set your memory_limit to higher value..
  • depends on the memory usage of your
    script also
  • max_execution_time could be a problem


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜