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>
<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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With