Cross domain XMLHttp request in Phonegap

Status
Niet open voor verdere reacties.

gast0659

Gebruiker
Lid geworden
27 dec 2009
Berichten
42
Hallo allen,

Ik ben nu bezig met een Phonegap app met jquerry mobile, waarin ik graag een xml bestandje van mijn server zou willen parsen naar een aantal variabelen in mijn javascript. Ik heb nu het volgende:

[JS] if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","dehoek.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
x=xmlDoc.getElementsByTagName("product");

document.write('<form action="http://scoutline.org/test/mailer.php" method="POST" data-ajax="false">');
document.write('<div data-role="fieldcontain">');
document.write('<fieldset data-role="controlgroup" data-type="vertical" data-mini="true">');
for (var i=0;i<x.length;i++)
{
document.write('<input name="check[]" id="checkbox" type="checkbox" value="'+(x.getElementsByTagName("naam")[0].childNodes[0].nodeValue)+'" /><label for="checkbox">'+(x.getElementsByTagName("naam")[0].childNodes[0].nodeValue)+' &nbsp; &nbsp;'+(x.getElementsByTagName("prijs")[0].childNodes[0].nodeValue)+'</label>');
}
document.write('</fieldset>');
document.write('</div>');
document.write('<input type="submit" data-theme="a" value="verstuur" name="verstuur" data-mini="true" /></form>');[/JS]

Alles werkt prima en alles wordt netjes verstuurd, als ik hem gewoon als website draai. Wanneer de XML echter van een externe server moet komen echter niet meer. Ook na een dik uurtje googlen ben ik niets wijzer geworden, wat ik echter wel weet ondertussen, is dat verschillende browsers het op verschillende manieren aanpakken. Mijn vraag is nu, hoe pakt een Phonegap app dat aan, en hoe maak ik dit stuk code daarvoor geschikt.
Hier is trouwens nog de xml die ik gebruik.

Alvast bedankt,
Jan Thiemen
 
Vanwege beveiligings risico's kunnen browsers alleen AJAX calls doen naar het domein waar de HTML ook vandaan komt, hun "eigen" domein dus.
Moet exact dezelfde domein naam zijn.

De oplossing voor als je AJAX calls naar een andere server wilt doen is een PHP Curl script dat de call voor je forward, vervolgens roep je dat script aan vanuit je javascript..

Moet je natuurlijk wel CURL voor geinstalleerd hebben, maar zelfs op shared cheap servers is dat tegenwoordig al voor je gedaan.
<?php phpinfo();?> en googlen naar "php configure curl" are your friends..

forward_ajax_call.php:
Code:
<?php

/**
 * Transport for Cross-domain AJAX calls
 *
 * This is an implementation of a transport channel for utilizing cross-domain
 * AJAX calls. This script is passed the data through AJAX along with two special
 * hidden field containing the action URL and the http method (GET/POST). It then  
 * sends the form fields to that URL and returns the response.
 *
 * @package		CrossDomainAjax
 * @category	CURL
 * @author		Md Emran Hasan <phpfour@gmail.com>
 * @link		http://www.phpfour.com
 */


// The actual form action
$action = $_REQUEST['url'];
// Submission method
$method = $_REQUEST['method'];

// Query string
$fields = '';

// Prepare the fields for query string, don't include the action URL OR method
if (count($_REQUEST) > 2)
{
    foreach ($_REQUEST as $key => $value)
    {
	    //htmlDump ($value, $key);
        if ($key != 'url' || $key != 'method')
        {
            $fields .= $key . '=' . urlencode($value) . '&';
        }
    }
}

// Strip the last comma
$fields = substr($fields, 0, strlen($fields) - 1);

if (!function_exists('curl_init')) die('please install cURL');

// Initiate cURL

$ch = curl_init();
// Do we need to POST of GET ?
if (strtoupper($method) == 'POST')
{   
    curl_setopt($ch, CURLOPT_URL, $action);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
}
else
{
    curl_setopt($ch, CURLOPT_URL, $action . '?' . $fields);   
}


// Follow redirects and return the transfer
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Get result and close cURL
//tmlDump ($action);
$result = curl_exec($ch);

curl_close($ch);
// Return the response
echo $result;
?>
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan