0

I have a very simple PHP form which posts XML data to a URL, which then responds with more XML. Here it is:

form action="[...]" method="POST">

        <textarea name="xml" rows="30" cols="100">
            <RRequest version = '2.13'>
                <Request>
                    <Command>update</Command>
                    <Pd>
                        <RID>1</RID>
                        <ExtID>111111111</ExtID>
                    </Pd>
                </Request>
            </RRequest>

        </textarea>
        <br>
        <input value="Submit Request" type="submit">

    </form>

This works beautifully. I am attempting to implement the same thing, but using jQuery. Here is that code:

        <script>

        $(document).ready(function() {

            $("#submit").click(function(){

                var xml_data = $("#xml").val();

                $.ajax({
                    type: "POST", 
                    url: "[...]", 
                    data: xml_data,
                    contentType: "text/xml",
                    dataType: "text",
                    cache: false,
                    success: function(data){
                        $("#xml").val("");
                        $("#xml").val(data);
                    },
                    error: function(xhr, textStatus, errorThrown){
                        alert("it no works");
                    }
                });

            });

        });

    </script>

</head>

<body>

        <textarea id="xml" rows="30" cols="100">
            <RRequest version = '2.13'>
                <Request>
                    <Command>update</Command>
                    <Pd>
                        <RID>1</RID>
                        <ExtID>111111111</ExtID>
                    </Pd>
                </Request>
            </RRequest>

        </textarea>
        <br>
        <input type="button" id="submit" />

This, however, does not work. It does contact the server, but the server returns a bad request message, which makes me think the data being sent is not correct. The two textareas containing the XML are identical. Any help would be greatly appreciated.

I am using jQuery 2.1.3.

1 Answers1

1

replace this line

  var xml_data = $("#xml").val();

with

  var xml_data = {xml: $("#xml").val() };

UPD: these parameters should be removed from your .ajax() request:

  contentType: "text/xml",
  dataType: "text",
Andriy B.
  • 421
  • 3
  • 14