0

First of all, I have already read this answer, which is to do the Cross-Domain Ajax GET request with php proxy. But what I need is a Ajax POST request.

So in my project, long time ago. Someone wrote this php file and together the ajax call in JavaScript, those are mean to solve the cross origin problem and which works really good! So I never think about to understand this, because I basiclly just need to change the url in the JavaScript and don't need to understand how this Ajax call works together with php.

PHP:

<?php
$nix="";
$type=$_GET['requrl'];
if ($_GET['requrl'] != $nix) {
    $file = file_get_contents($_GET['requrl']);
}
elseif ($_POST['requrl'] != $nix) {
    $file = file_get_contents($_POST['requrl'], false, $_POST['data']);
}
else {
    $file = "false type";
}
echo $file;
?>

JavaScript:

var url = "https://XXXXXXXXXXXXXX";
url = encodeURI(url); 
var useProxyPhp = true;
    var data = (useProxyPhp) ? {requrl: url} : "";
    var ajaxUrl = (useProxyPhp) ? "proxy.php" : url;

var ajaxProperties = {
    type: 'GET',
    data: data,
    url: ajaxUrl,
    cache: false 
};

res = jQuery.ajax(ajaxProperties).done(function(res) {
    // do something with the result here
})

So what I need to do is just take the same ajax GET request (copy and paste in JS) and just replace the url every time ==> job done!

Now, the first time I need to do a ajax POST request to send a xml file to the server, the server will do some calculate on it and give me a response.

I tested first with the POSTMAN and everything works fine, but when I switch to my real project. I become the Cross origin problem. So I think If I could do something to the already existing php and js, so I can solve the cross origin problem.

I tried this in my JavaScript but I got only the "false type" as antwort

function sendWPSRequest(xml) {
var url = "https://XXX.XXX.XXX.XXX:XXXX/wps";
useProxyPhp = true;
    var data = (useProxyPhp) ? {requrl: url, data: xml} : "";
    var ajaxUrl = (useProxyPhp) ? "proxy.php" : url;

$.ajax({
    type: "POST",
    url: ajaxUrl,
    dataType: "text",
    contentType: "application/xml",
    data: data,
    success:function (response) {
        console.log('POST success: ', response);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert("POST", textStatus, errorThrown);
    }
});
}

Can someone help me a little bit to understand what the php is doing here and what should I do to modify the php and JS.

Min XIE
  • 463
  • 8
  • 19
  • Might you show us which headers are present in the reponse from the external website? The relevant ones begin with `Access-Control-*`. – Tobias F. Aug 23 '18 at 13:22
  • @TobiasF. Sorry, I can't find anything related to `Access-Control-*` because I think the request was never sent to the server, it already gets wrong with the php proxy. When I do the ajax POST request without the proxy php, then I become the warning that firefox block the my Cross-Origin request to the url I want. – Min XIE Aug 23 '18 at 13:32

0 Answers0