I'm using jQuery.ajax()
to submit a form on my website. I get an .xml response and I want to send it to a .php file. I've tried using another jQuery.ajax()
function:
<script type="text/javascript">
$(document).ready(function() {
$('#form').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'myURL',
data: $('#form').serialize(),
success: function(xml)
{
$.ajax({
type: 'POST',
url: 'saveData.php',
data: {
xml: $(xml),
},
success: function(data) {
$('#output').html(data);
}
});
}
});
});
})
</script>
but I faced "Illegal invocation"
error. So I added processData: false
to my inner AJAX function. Now I get another error: Undefined index: xml in...
Here is my PHP code:
<?php
$xml = $_POST['xml'];
?>
How to manage this issue?
What you can try is specify in the first $.ajax
call that you are going to receive an XML using dataType
:
$.ajax({
type: 'POST',
url: 'myURL',
dataType: 'xml,
data: $('#form').serialize(),
success: function(xml) (...)
But I have a question. Why using an ajax into an other ajax call ? If both are made to the same server, it dont seems legit.
This can help you to debug. Maybe if you change xml: $(xml) for xml: $(xml).val() you get what you wanted.
Uncoment the line alert($(xml).val()) and you will see what is the content of $(xml).
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#form').submit(function(e)
{
e.preventDefault();
// alert($(xml).val());
$.ajax({
type: 'POST',
url: 'testpost.php',
data: {
xml: $("#xml").val(),
},
success: function(data) {
$('#output').html(data);
}
});
});
})
</script>
</head>
<body>
<form id="form" method="POST">
<textarea name="xml" id="xml"><myxml>this is a test</myxml></textarea>
<input type="submit" value="Send">
</form>
<div id="output">Nothing executed till now</div>
</body>
</html>
testpost.php
<?php
var_dump($_POST["xml"]);