{"id":3146690,"date":"2012-10-26T13:53:53","date_gmt":"2012-10-26T13:53:53","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/cross-domain-ajax-request\/"},"modified":"2016-08-20T16:59:43","modified_gmt":"2016-08-20T16:59:43","slug":"cross-domain-ajax-request","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/cross-domain-ajax-request\/","title":{"rendered":"Cross-Domain Ajax request"},"content":{"rendered":"<p>Hi everybody. I try to send an ajax-request to a different server than where my wordpress installation is hosted. This site returns a xml file which I need to process.<\/p>\n<p>So this is the originial form how to snd things to the provider:<\/p>\n<pre><code>&lt;h3&gt;Step 2: XML authorizeSplit (server-2-server request)&lt;\/h3&gt;\n\t        &lt;form action=&quot;https:\/\/...\/file.jsp&quot; method=&quot;post&quot; target=&quot;_parent&quot;&gt;\n\t            &lt;fieldset&gt;\n\t                &lt;textarea style=&quot;width: 400px; height: 150px;&quot; name=&quot;xmlRequest&quot; id=&quot;xml-request&quot;&gt;&lt;\/textarea&gt;\n\n\t                &lt;div&gt;\n\t                \t&lt;input type=&quot;submit&quot; class=&quot;button&quot; value=&quot;send&quot; id=&quot;submit_payment&quot; \/&gt;&lt;span class=&quot;buttend&quot;&gt;&lt;\/span&gt;\n\t                &lt;\/div&gt;\n\t            &lt;\/fieldset&gt;\n\t        &lt;\/form&gt;<\/code><\/pre>\n<p>So I reformulated this into a typical wordpress ajax call:<\/p>\n<p>jQuery Code:<\/p>\n<pre><code>$(&#039;#step2 #submit_payment&#039;).click(function() {\n\n\t\t\t\tvar xmlRequest = $(&#039;#step2 #xml-request&#039;).val();\n\t\t\t\tvar ajax_url = $(&#039;#step2&#039;).find(&#039;input[name=&quot;ajax_url&quot;]&#039;).attr(&#039;value&#039;);\n\n\t\t\t\t$.ajax({\n\t\t\t\t\ttype: &#039;POST&#039;,\n\t\t\t\t\tdata: &#039;action=redirect_to_external&amp;xml_request=&#039; + xmlRequest,\n\t\t\t\t\turl: ajax_url,\n\t\t\t\t\t\/\/async: false,\n\t\t\t\t\tbeforeSend: function() {\n\t\t\t\t\t\t\/\/ display loading animation container\n\t\t\t\t\t},\n\t\t\t\t\terror: function(responseData, textStatus, errorThrown) {\n\t\t\t\t\t\talert(textStatus);\n\t\t\t\t\t},\n\t\t\t\t\tsuccess: function(data) {\n\n\t\t\t\t\t\talert(data);\n\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t});<\/code><\/pre>\n<p>PHP function the ajax request refers to:<\/p>\n<pre><code>function redirect_to_external() {\n\n\t$xml_request = $_POST[&#039;xml_request&#039;];\n\theader(&quot;Location: https:\/\/...\/file.jsp?xmlRequest=&quot;.urlencode($xml_request));\n\n}\nadd_action(&#039;wp_ajax_redirect_to_external&#039;, &#039;redirect_to_external&#039;);\nadd_action(&#039;wp_ajax_nopriv_redirect_to_external&#039;, &#039;redirect_to_external&#039;);<\/code><\/pre>\n<p>So the callback is always giving me an error. But in general the ajax call works locally. In case I replace the header statement inside my php function and just echo some text, this is returned. So my question is how can I send my xml request to the external server. Is there any chance to do it this way or do I have to setup a proxy functionality or something else?<\/p>\n<p>Any hints are very welcome.<br \/>\nThanks in advance.<\/p>\n<p>Cheers, kool<\/p>\n","protected":false},"template":"","class_list":["post-3146690","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/3146690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/3146690\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=3146690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}