Wordpress 301 Moved Permanent Response to Jquery.post Request

I have lost my hope of finding a solution to my problem, I hope someone can help me here. Here's the problem:

I have a wordpress installation with an extra wp_lojas table (for stores). Customers can go to the site and through the form, put their address and use Google Maps Api, find stores near them.

When a user submits a form, I check its address position (latitude / longitude), and through an SQL query, I look for the nearest stores.

The form sends the parameters (latitude / longitude / radius) through the ajax request ($ .post ()) to the php file that is inside my theme folder (/wp-content/themes/accessorize/dadosLojas.php) and this file creates XML with found repositories.

Everything works fine offline, on my local machine. On the net, I get the answer "301 moved forever." If you guys installed Firebug and want to try it, the test link is http://www.colletivo.com.br/accessorize/ and the form is located on the page footer. If you want to get the address from Brazil, try "Rua Vicente Leporace, 1534".

If you do not understand what I was trying to explain, or if you need more information, please let me know.

Many thanks.

+5
source share
3 answers

Problem Solved with a hint that Greg Pettit gave!

WordPress ajax-, , , htaccess URL-.

, , ajax, Wordpress, :

functions.php:

// Hooks wp_ajax that points to the function that builds the XML Stores
add_action('wp_ajax_procura_lojas','procuraLojas'); // Unlogged User
add_action('wp_ajax_nopriv_procura_lojas','procuraLojas'); // Logged User

// Function that Builds Stores XML
function procuraLojas() {

    global $wpdb;

    // Retrieve $_POST informattion coming from jQuery.post
    $lat = $_POST["latitude"];
    $lon = $_POST["longitude"];
    $raio = $_POST["raio"]; 

    // Query wp_lojas and Build XML
    $consulta = $wpdb->get_results(sprintf("SELECT * , ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distancia FROM wp_lojas HAVING distancia < '%s' ORDER BY distancia",
    mysql_real_escape_string($lat),
    mysql_real_escape_string($lon),
    mysql_real_escape_string($lat),
    mysql_real_escape_string($raio)));

    $dom = new DOMDocument("1.0", "utf-8");
    $no = $dom->createElement("lojas");
    $parnode = $dom->appendChild($no);

    header("Content-type: text/xml");

    foreach ($consulta as $loja){

        $no = $dom->createElement("loja");
        $novono = $parnode->appendChild($no);

        $novono->setAttribute('nome',           $loja->nome);
        $novono->setAttribute('lat',            $loja->latitude);
        $novono->setAttribute('lon',            $loja->longitude);
        $novono->setAttribute('telefone',       $loja->telefone);
        $novono->setAttribute('email',          $loja->email);
        $novono->setAttribute('endereco',       $loja->endereco);
        $novono->setAttribute('numero',         $loja->numero);
        $novono->setAttribute('complemento',        $loja->complemento);
        $novono->setAttribute('bairro',         $loja->bairro);
        $novono->setAttribute('cidade',         $loja->cidade);
        $novono->setAttribute('estado',         $loja->estado);
        $novono->setAttribute('cep',            $loja->cep);
        $novono->setAttribute('distancia',      $loja->distancia);

    }

    // Print XML and Exit   
    echo $dom->saveXML();
    exit;

}

Wordpress :

<script type='text/javascript'>
/* <![CDATA[ */
var MyAjax = { ajaxurl: "<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php" }; // Build the Link to admin-ajax.php / Javascript Global Variable
/* ]]> */
</script>

, POST ajax Wordpress, "wp-admin/admin-ajax.php":

jQuery.post(
    MyAjax.ajaxurl, // Link to the file 'wp-admin/admin-ajax.php' responsible for handling ajax requisitions
    {
        action : 'procura_lojas', // Name used in the hook 'wp_ajax_procura_lojas'
        latitude : center.lat(), // Latitude Parameter
        longitude : center.lng(), // Longitude Parameter
        raio : raio // Radius Parameter
    },
    function(data) { // Callback

        // Retrieve all nodes called 'loja' and put it in the map
        var markers = data.documentElement.getElementsByTagName("loja");
        for (var i = 0; i < markers.length; i++) {
            var dados = [];
            dados["nome"] = markers[i].getAttribute('nome');
            dados["estado"] = markers[i].getAttribute('estado');
            dados["cidade"] = markers[i].getAttribute('cidade');
            dados["bairro"] = markers[i].getAttribute('bairro');
            dados["endereco"] = markers[i].getAttribute('endereco');
            dados["numero"] = markers[i].getAttribute('numero');
            dados["complemento"] = markers[i].getAttribute('complemento');
            dados["cep"] = markers[i].getAttribute('cep');
            dados["telefone"] = markers[i].getAttribute('telefone');

            var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute('lat')), parseFloat(markers[i].getAttribute('lon')));
            var marker = createMarker(markers[i].getAttribute("name"), latlng, dados);
        }
    }
);

! .

, , 5 ajax wordpress

, -.

+1

Wordpress AJAX PHP. , - , , . , AJAX script, . ajax-admin.php( admin-ajax.php?)

, , , , , AJAX script Wordpress.

0

, . - , .

function preventAccessToBackend() {    
    if(!current_user_can('edit_posts')) {
        wp_redirect(home_url()); exit;
    }
}
add_action('admin_init', 'preventAccessToBackend');

, admin_init , , , AJAX.

. , AJAX- .

function preventAccessToBackend() {    
    if (defined('DOING_AJAX') && DOING_AJAX) { return; }

    if(!current_user_can('edit_posts')) {
        wp_redirect(home_url()); exit;
    }
}
add_action('admin_init', 'preventAccessToBackend');
0
source

All Articles