0

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?

always_bloo
  • 165
  • 5
  • 13

2 Answers2

0

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.

ThinkTank
  • 1,187
  • 9
  • 15
  • Unfortunately it didn't work. 'myURL' is an external server. – always_bloo Dec 06 '16 at 14:21
  • Ok if its an external server. Did you try to mix my answer with @Jai comment about using `xml : xml` instead of `xml : $(xml)` in the second $.ajax ? – ThinkTank Dec 06 '16 at 14:25
  • Yes, I did and it also didn't help. – always_bloo Dec 06 '16 at 14:34
  • You said you're using an external server. If you're using two diferent servers, maybe you will have to deal with XSS. There's an answer in Stackoverflow suggesting to use JSONP to overcome the issues of cross site Scripting: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Antonio Alexandre Dec 06 '16 at 15:18
0

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"]);