views:

42

answers:

2

Hi,

I'm quite new to the whole jQuery/JSON thing but thought I would give it a go. The idea is that I am posting data to a PHP script and then returning a JSON object. This works fine on my localhost but on the web server, Firebug shows that the JSON object is being returned but I also get a 404 error.

Any ideas where I could be going wrong?

Javascript -

    $(".vote").click(function(){
 $('#graph').empty();
 var area = $(this).attr("id");
 $.ajax({
  dataType: "json",
  type: "POST",
  url: "<?php echo base_url(); ?>home/vote",
  cache: false,
  data: "area="+ area,
  success: function(json){

   arrayOfData = new Array(
    [json.science_graph,'Science','#009999'],
    [json.maths_graph,'Maths','#FF6600'],
    [json.ict_graph,'ICT','#FF0000'],
    [json.mfl_graph,'MFL','#FFCC00'],
    [json.dt_graph,'Design Technology','#33CC00'],
    [json.other_graph,'Other Events','#003399']
   ); 
   $('#graph').jqBarGraph({ data: arrayOfData, barSpace: 5, width: 430 });

  }
 }); 
});

PHP -

 if ($vote == true)
   {
    $poll = $this->ts_model->graph_poll();

    list($maths, $science, $ict, $dt, $mfl, $other) = $poll;

    echo "{";
    echo "\"science_graph\":\"".$science."\",";
    echo "\"ict_graph\":\"".$ict."\",";
    echo "\"dt_graph\":\"".$dt."\",";
    echo "\"other_graph\":\"".$other."\",";
    echo "\"mfl_graph\":\"".$mfl."\",";
    echo "\"maths_graph\":\"".$maths."\"";
    echo "}";
   }

Thanks in advance.

A: 

What is ".vote"? I suggest changing that "click" handler so that it has

return false;

as the last line. My (barely warranted) guess is that ".vote" is either a submit button or an <a> tag, and that the "click" is triggering both your handler and the native action. If the handler returns false then the native action won't proceed.

Pointy
".vote" is an <a> tag. I have attempted to add return false; but this is not having any effect.
Tom
Are you sure you added `return false;` to the "click" handler function itself, and not the AJAX response handler? You can use something like the Firefox TamperData plugin to see *all* the HTTP requests your page makes. That might help you see if the problem is that your page is submitting *two* requests instead of one. Or, as an experiment, you can change the "href" on the `<a>` tag to be some real URL (http://google.com or whatever).
Pointy
Thank you for your response, I did everything you said and checked ot TamperData - very good plugin. It turns out in the end that by including a wordpress code snippet <?phprequire('/the/path/to/your/wp-blog-header.php');?> it was stopping the JSON request from displaying properly.
Tom
Cool, I'm glad it works. TamperData is indeed a very useful "sanity check" tool to have around.
Pointy
A: 

If the json call is returning 404 which is a I think what you are saying you need to check that this is working ok. You should be able to put the url you are calling in your browser to get a better idea of what is going on. Once you do this you will be able to tweak it and then add it back into your script.

matpol
I have tested the url for the JSON request and it returns the JSON string - {"science_graph":"3","ict_graph":"3","dt_graph":"7","other_graph":"4","mfl_graph":"4","maths_graph":"2"}.Firebug is also showing that this is returned when the ajax is fired, however firebug also shows a 404 and the data isn't loaded back into the page which I don't understand.
Tom