How to implement cross-domain Ajax query using CakePHP and jQuery?

I use CakePHP for my project, and I create XML views so that I can interact with them (CRUD) from an external website. CakePHP website requires authentication.

Essentially, I want to view " http://mycakeapp.com/posts/views/1.xml " from " http://www.example.com "

However, I get this error when using the jQuery ajax function: Access to the restricted URI is denied by "code:" 1012 . It seems from googling that perhaps a JSONP attempt is an option, but it is not native to the cake, and so I would rather use xml :(

I tried using iframe: it loads the login screen - and after logging in it loads the current page (for example, http://www.example.com ")! Despite the fact that the iframe source is http: // mycakeapp. com / posts / views / 1.xml "

Has anyone solved this problem before?

Update: To be more specific, I would like to create a bookmarklet that links to my site (built on CakePHP), so the url proxy method will not work (but thanks for the suggestion)

+3
source share
3 answers

JSONP is definitely what you are looking for.

, : http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

0

script PHP. Ajax - script, script "" cURL, - script cURL, .

+4

mmattax, - script.

script , urlencoded proxy_url.

url_proxy.php   

// Is it a POST or a GET?
$url = ($_POST['proxy_url']) ? $_POST['proxy_url'] : $_GET['proxy_url'];

// Open the Curl session
$session = curl_init($url);

// If it a POST, put the POST data in the body
if ($_POST['proxy_url']) {
    $postvars = '';
    while ($element = current($_POST)) {
        if (key($_POST) != 'proxy_url') {
            $postvars .= key($_POST).'='.$element.'&';
        }
        next($_POST);
    }
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
}

// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Make the call
$response = curl_exec($session);

// Return the response
if (curl_errno($session)) {
    $error_message = curl_error($session);
    echo 'Error: '.$error_message;
} else {
    echo $response;
}

curl_close($session);
?>

, xml, .

webroot javascript - :

function showMapLegend(baseURL, layer) {
    var url = 'http://our-map-server/get-a-legend.php?layer='+layer;
    var dt = new Date();
    var proxy = baseURL + '/url_proxy.php?currDate='+dt.getTime()+'&proxy_url=';
    url = proxy + encodeURIComponent(url);

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            $('map-legend-img').src = transport.responseText;
            new Effect.Appear('map-legend', {duration: 0.5});
        }
    });
}

The javascript function example above is used to get a simple URL string back from our map server, we don’t care if it doesn’t work, therefore there is no onFailure, etc., and basically it is Prototype, but I'm sure you got an idea of ​​how this uses a proxy script.

The baseURL variable is passed inside, it should contain the base http: // server / theappname ", as the url for your application.

+3
source

Source: https://habr.com/ru/post/1705001/


All Articles