WordPress.org

Ready to get started?Download WordPress

Forums

Ajax using Google Maps API call returning -1 (7 posts)

  1. AJ Mallory
    Member
    Posted 1 year ago #

    I'm struggling with using ajax calls in wordpress. I know my ajax calls were working prior to migrating the code into WordPress so it's really only the wp ajax that is in question. If I enter http://localhost/wp-admin/admin-ajax.php?wwta_site_fill into my browser address bar I'm getting back -1. I don't understand why my function isn't being found/called.

    below is the php code from my main plugin file which is setting up the ajax stuff (I've tried to trim my code down as much as I could leaving only what's relevant):

    add_action('wp_ajax_ta_site_fill', 'ta_ajax_site_fill');
    add_action('wp_ajax_nopriv_ta_site_fill', 'ta_ajax_site_fill');
    
    function ta_ajax_site_fill(){
    	// qurey DB and build xml data
    	...snip...
    	// Start XML file, create parent node
    	$dom = new DOMDocument("1.0");
    	$node = $dom->createElement("marker");
    	$parnode = $dom->appendChild($node);
    	...snip...
    	echo $dom->saveXML();
    	die();
    }
    
    function ta_map_load_scripts(){
    	$ta_options = get_option('ta_map_options');
    	wp_register_script('ta_gmap_ref', 'http://maps.googleapis.com/maps/api/js?key=' . $ta_options["api_key"] . '&sensor=false',array(),NULL);
    	wp_register_script('ta_gmap_local_js',WP_PLUGIN_URL.'ta_site_map.js',array(jquery),NULL);
    	wp_enqueue_script('ta_gmap_ref',array(),NULL);
    	wp_enqueue_script('ta_gmap_local_js',plugins_url('ta_site_map.js',__FILE__),array(),NULL);
    	wp_localize_script('ta_gmap_local_js','wp_ta_map_vars',array(
    		'local_path'=>plugins_url(__file__),
    		'ajaxurl'=> admin_url('admin-ajax.php')
    	));
    }
    
    add_action('wp_enqueue_scripts','ta_map_load_scripts');

    Here's my js I'm using google maps javascript api.

    //<![CDATA[
        function gmap_load() {
          var map = new google.maps.Map(document.getElementById("map"), {
            center: --snip--
          });
          var infoWindow = new google.maps.InfoWindow;
          downloadUrl( wp_ta_map_vars.ajaxurl + "?ta_site_fill", function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < element1.length; i++) {
              var ---snip---;
              var baloonhtml = "<b> <a target=\"_blank\" href="+wp_wwta_map_vars.local_path+"/site_detail_template.php?siteID=" + siteID + ">" + name + "</a> </b> <br/>" + steloc;
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                title: name
              });
              bindInfoWindow(marker, map, infoWindow, baloonhtml);
            }
          });
        }
    
        function bindInfoWindow(marker, map, infoWindow, html) {
          google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
          });
        }
        function downloadUrl(url, callback) {
          var request = window.ActiveXObject ?
              new ActiveXObject('Microsoft.XMLHTTP') :
              new XMLHttpRequest;
          request.onreadystatechange = function() {
            if (request.readyState == 4) {
              request.onreadystatechange = doNothing;
              callback(request, request.status);
            }
          };
          request.open('GET', url, true);
          request.send(null);
        }
        function doNothing() {}
        //]]>

    Thank you taking the time to look at my issue,
    Jason

  2. AJ Mallory
    Member
    Posted 1 year ago #

    So I've gone as simple as I can think of and I'm still not able to get my ajax call to work. Here's my php code that is in my_plugin.php

    add_action('wp_ajax_my_call', 'my_call');
    add_action('wp_ajax_nopriv_my_call', 'my_call');
    
    function my_call(){
    	die("hello?");
    }

    I call it directly from the browser address bar like this "http://localhost/wp-admin/admin-ajax.php?my_call" and I'm still getting "-1" as a response.

    What am I missing?

  3. bcworkz
    Member
    Posted 1 year ago #

    If you are using WP version 3.4.*, the wp_ajax_* actions have been depreciated. I'm unaware of an appropriate work around. I'm not saying there isn't one, just that I don't know of one.

  4. AJ Mallory
    Member
    Posted 1 year ago #

    bcworkz, thanks for your response, I suppose wp_rewrite and a seperate .php would be one way to work around not using wp_ajax_* actions.

    Regardless in case someone else stumbles on this post, I found that if I include a nonce wp_create_nonce('my_nonce') things start to work.

    Seams there is a lot of documentation out there that makes it seam like using a nonce isn't mandatory. Maybe there is a way around it and maybe is wasn't required in the past. I couldn't get wadmin-ajax.php to accept any request without it. Once I started including in for my script and adding it to the manual request in the browser address bar things worked. I added it to my wp_localize_script to get it to my js code.

  5. Andrew Bartel
    Member
    Posted 1 year ago #

    Bcworkz where did you see that those hooks are deprecated?

    Basic ajax example, expand as you need, this does not include nonces. This will replace the content of div baz.

    Some page:

    <div id="baz">Click me</div>

    A js file:

    jQuery(document).ready(function() {
      jQuery(#baz).click(function() {
        data = {};
        data.action = 'your_ajax_action';
        data.content = 'some content to replace Click Me';
        jQuery.post('/wp-admin/admin-ajax.php',data,your_ajax_callback);
      });
    })
    
    function your_ajax_callback(return) {
      jQuery('#baz').html(return);
    }

    In your plugin/functions.php

    add_action('wp_ajax_your_ajax_action' , 'the_php_ajax');
    add_action('wp_ajax_nopriv_your_ajax_action' , 'the_php_ajax');
    
    function the_php_ajax() {
    // $_POST['content'] == data.content;
      echo $_POST['content'];
      die(); // you must put a die after your echo/print
    }
  6. Andrew Bartel
    Member
    Posted 1 year ago #

  7. bcworkz
    Member
    Posted 1 year ago #

    My reference was http://adambrown.info/p/wp_hooks/hook/wp_ajax_%7B$_POST%5Baction%5D%7D

    Sorry for the mis-information, I'm glad the real issue was identified.

Topic Closed

This topic has been closed to new replies.

About this Topic