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.

5
  • Is it posting to a remote server? Commented Apr 9, 2015 at 20:48
  • @Jon Yes it is. The files I'm working on are kept on my local machine, but they are posting to an absolute url (https). Commented Apr 9, 2015 at 20:50
  • stackoverflow.com/questions/2722750/ajax-datatype . I think dataType should be XML.. Commented Apr 9, 2015 at 20:59
  • I don't believe you can post from AJAX to a different server. XSS I believe is the name to look for regarding that. Commented Apr 9, 2015 at 21:34
  • sings What does the Fiddler say? Commented Apr 9, 2015 at 21:37

1 Answer 1

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",
Sign up to request clarification or add additional context in comments.

10 Comments

Thank the old gods and the new. Well done, friend. Thank you.
Just curious, do you see any reason this would work in Safari, but not Firefox?
@user1647550, yes, this code will work in any browser
That's what I thought too. It works flawlessly in Safari, but hits the error handler immediately in Firefox and Chrome.
show us the error, please. probably, you need to put xml param into quotes, i.e. { "xml": ... }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.