开发者

How to post to a PHP script using Prototype.js

I'm brand new to prototype.js and trying to do a simple GET request to a PHP script. The Javascript show do the following:

  1. Get the users latitude and longitude
  2. Pass the lat/long to the doStuff() function
  3. Pass the lat/long to my PHP script (savecoords.php) to be saved.

For some reason, it's like this code isn't even executing even though I am sure I'm using an HTML5 capable browser that implements the Geolocation API. Here is the code:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        doStuff(position.coords.latitude, position.coor开发者_Go百科ds.longitude);
    });
}

function doStuff(mylat, mylong) {
    new Ajax.Request('savecoords.php', {
        method: 'get',
        parameters: {
            'latitude': myLat,
            'long': myLong
        }
    }
}

Any ideas?


In order to figure out why your function is not being called, you'll likely need to pass in an error callback. For example:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(yourFunction, function (error) {
        console.log(error.message, error.code);
    });
}

This will provide you with the error code and message to narrow down why your function isn't being called. The code will return one of several values, and the message should provide greater detail. From the MDC page linked above:

UNKNOWN_ERROR (numeric value 0)

The location acquisition process failed due to an error not covered by the definition of any other error code in this interface.

PERMISSION_DENIED (numeric value 1)

The location acquisition process failed because the application origin does not have permission to use the Geolocation API.

POSITION_UNAVAILABLE (numeric value 2)

The position of the device could not be determined. One or more of the location providers used in the location acquisition process reported an internal error that caused the process to fail entirely.

TIMEOUT (numeric value 3)

The specified maximum length of time has elapsed before the implementation could successfully acquire a new Position object.

I'm sure that this doesn't solve your problem entirely, but it should give you the necessary insight to make some progress.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜