4 Replies - 1754 Views - Last Post: 16 March 2013 - 01:11 AM

#1 g37752   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

.post gets no response?

Posted 15 March 2013 - 11:05 AM

My php code is not responding?

$(document).ready(function(){
  $("#show").click(function(){

  var period=$("#period").val();
  alert(period);     // shows '32' correctly
  $.post("http://example.com/getcharts.php",{"period":period},function(result){
       alert(result);
        ....
   },"json");
  })
})


getcharts.php:
<?php
$period=$_POST['period'];
echo 'period='.$period;
... generate JSON data...
?>

This post has been edited by Dormilich: 16 March 2013 - 02:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: .post gets no response?

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: .post gets no response?

Posted 15 March 2013 - 12:34 PM

Your page is expected to return correctly formed json data, so I would remove this line:

echo 'period='.$period;

and return some sample json.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: .post gets no response?

Posted 15 March 2013 - 12:43 PM

You might use the following sample code to return json data:

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

and then confirm this in your page with:

alert(JSON.parse(result));
// or
var jResult = JSON.parse(result);
alert(jResult['a']);    // 1

Was This Post Helpful? 1
  • +
  • -

#4 g37752   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: .post gets no response?

Posted 15 March 2013 - 03:40 PM

says 'JSON is undefined?
i check my encoded result, which looks right:
{"13031523":"02,11,03,01,10,27,10,2:3,3:2,4:1",....}

i am using IE8. i copy these 4 files (https://github.com/douglascrockford/JSON-js), except for json.js, and added
<script type='text/javascript' src="http://domain.com/json2.js"></script>


now i am getting 'exception thrown and not caught in jquery-1.6.2.min.js?'

i made sure it's <!DOCTYPE html>
without json2.js, i am getting syntax error on
var jResult = JSON.parse(result);

This post has been edited by Dormilich: 16 March 2013 - 02:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: .post gets no response?

Posted 16 March 2013 - 01:11 AM

If the error says 'JSON is undefined' then try firstly with just alert(result);, to check whether anything is returned. Test in other browsers as well.

I cannot recall whether JSON.parse() is necessary - it either is or it isn't! Alternatively, you might revert to

var jResult = eval('(' + result + ')');    // which shouldn't really be used; or jQuery:
$.parseJSON(result);

Otherwise, you should learn to debug using your browsers' Console; there are links in my signature.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1