AJAX submit form to remote URL and extract URL from XML reponse

后端 未结 3 1712
走了就别回头了
走了就别回头了 2021-01-26 14:19

I am new to JavaScript and Ajax.

I have a form which posts to remote url and returns a XML response containing some data and a URL. I need to extract the url and redirec

3条回答
  •  北海茫月
    2021-01-26 14:56

    Yes, due to the same origin policy restriction you cannot use javascript to perform this request. You could write a server side script that will perform the request to fetch the remote resource and then use an AJAX request to this server side script on your domain that will bring the XML and allow you to parse it.

    Obviously depending on the server side language you are using there might be different ways to implement this.

    But if you want to use jQuery it is pretty easy once you have setup this server side script on your domain. You simply use the $.ajax() method:

    $.ajax({
        url: '/url_to_your_server_side_script_that_serves_as_a_bridge',
        success: function(xml) {
            alert($('Result', xml).text());
        }
    });
    

    And here's a live demo.

提交回复
热议问题