POST request to UPDATE rows

梦想的初衷 提交于 2019-12-02 09:25:37

You must POST to the Fusion Tables API to update a column, but currently you can't do this directly from JavaScript as Google does not set the CORS headers to allow cross-domain posting to their API.

So if you want to do this, you have to "relay" your request through your webserver or something similar. I do it with the following PHP script, then I post the request via JavaScript to my PHP script. And finally, you have to authenticate yourself using OAuth.

$url = "http://www.google.com/fusiontables/api/query";
$relay = new PostRelay($url,$_POST);
echo $relay->relay();

And in class PostRelay the method relay() is defined, the $_POST holds all posted data (represented by $this->data in my class):

public function relay()
{
    $url = $this->getUrl();
    $this->curlHandler = curl_init($url);
    curl_setopt($this->curlHandler, CURLOPT_POST, true);
    curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->curlHandler, CURLOPT_TIMEOUT, 30);
    curl_setopt($this->curlHandler, CURLOPT_FOLLOWLOCATION, 1);

    if($this->data != null) {
        curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS, $this->getData());
    }

    $remote_answer = curl_exec($this->curlHandler);

    if(curl_errno($this->curlHandler) != 0) {
        $msgErrorNo = "cURL Errornumber: ".curl_errno($this->curlHandler);
        $msgError = "cURL Error: ".curl_error($this->curlHandler);
        throw new RemoteException($url,$msgErrorNo." ".$msgError);
    }
    return $remote_answer;
}

This is possible using the new Trusted Tester Fusion Table API. I've posted some code here:

http://groups.google.com/group/fusion-tables-api-trusted-testers/msg/126228f052eff30e?hl=en_US

To use the new code, you need to be a Trusted Tester. To become a Trusted Tester, simply join the Trusted Tester API group:

http://groups.google.com/group/fusion-tables-api-trusted-testers?hl=en_US

<!DOCTYPE html>
<html>
<head>
<style>
#map_canvas { width: 600px;height: 550px; }
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"type="text/javascript"></script>
<script type="text/javascript"src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var map;

var layer;
var tableid = 3385625;

function initialize() {
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: new google.maps.LatLng(-30.127460619520974, -51.167964935302734),
zoom: 11,
 mapTypeId: google.maps.MapTypeId.ROADMAP
});

layer = new google.maps.FusionTablesLayer(tableid);
layer.setQuery("SELECT 'ENDERECO' FROM " + tableid);
layer.setMap(map);
}
//FUNÇÃO PARA ATIVAR OS FILTROS
function changeMap() {
var searchString = document.getElementById('search-string').value.replace(/'/g, "\\'");
if (!searchString) {
layer.setQuery("SELECT 'ENDERECO' FROM " + tableid);
return;
}
layer.setQuery("SELECT 'ENDERECO' FROM " + tableid + " WHERE 'TIPO' = '" + searchString + "'");
var queryUrlHead = 'http://www.google.com/fusiontables/api/query?sql=';
var queryUrlTail = '&jsonCallback=?';
var query = "SELECT COUNT() FROM " + tableid + " WHERE 'TIPO' CONTAINS IGNORING CASE '" + searchString + "'"
var queryurl = encodeURI(queryUrlHead + query + queryUrlTail);
var jqxhr = $.get(queryurl,
function(data){
try{
$('#count').html((data.table.rows[0][0]));
}
catch(err){
$('#count').html('0')
}
},
"jsonp");
}

</script>

</head>
<body onload="initialize();">

<div id="map_canvas"></div>

<div style="margin-top: 10px;">
<label>Qual o tipo?</label>
<select id="search-string" onchange="changeMap(this.value);">
 <option value="">--Selecionar/Reset--</option>
 <option value="Bar">Bar</option>
 <option value="Comidas variadas">Comidas variadas</option>
 <option value="Espaço de Cultura">Espaço de Cultura</option>
 <option value="Hotel">Hotel</option>
 <option value="Igreja">Igreja</option>
 <option value="Museu">Museu</option>
 <option value="Restaurante">Restaurante</option>
 <option value="Shopping Center">Shopping Center</option>
 <option value="Teatro">Teatro</option>
</select>
 </div> </br>
 <label>Quantidade:</label>  
 <span id='count'></span>
 </body>
 </html>

Have a look at jQuery Ajax:

Example (assuming a .NET web service that resides at a web root folder called Ajax):

var Params = "{'myParamName': 'myParamValue'}";

$.ajax({
        type: "POST",
        url: "Ajax/MyWebService.asmx/MyWSMethod",
        data: Params,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            // Handle the response here as you need to...
        },
        failure: function () {
            // Handle failure if needed
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!