Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using onload() and ajax to get the array from php, but it didnt work. The html page should be able to get the array from n1.php and alert("GOOD"), but it's not giving any response, not even alerting GOOD or BAD so i really dont know what's wrong with the code. How can I fix this??

n1.html:

<!DOCTYPE html>
<html>
<body onload="getArr();">
here
</body>
<script type="text/javascript">             
function getArr(){              
    alert('return sent');   
    $.ajax({
        url: "n1.php",
        dataType: 'json',
        success: function(json_data){
        var data_array = $.parseJSON(json_data);
        var rec = data_array[0];
        alert("GOOD");
        },
        error: function() {
        alert("BAD");
        }
    });                 
}
 </script></html>

n1.php:

<?php
    $output = array("cat","dog");
    echo json_encode($output);
?>
share|improve this question
    
You do not need to parse JSON if you have specified dataType: json. Are you getting any error in console ? –  blunderboy 2 days ago
    
Are these the console error?? Uncaught SyntaxError: Unexpected token : n1.html:7 Uncaught SyntaxError: Unexpected token < www.serversfree.com/:1 Uncaught ReferenceError: getArr is not defined n1.html:2 –  user1887339 2 days ago
    
Have you included jquery? –  Vipin Soni 2 days ago
add comment

2 Answers

up vote 1 down vote accepted

The request must contains the type of request. Also the dataType refers on data you are going to send,as long as you don't send any data, it does not need here. Try this:

$.ajax({
  url: "n1.php",
  type: "GET",
  success: function(json_data){
    var data_array = $.parseJSON(json_data);
    var rec = data_array[0];
    alert("GOOD");
  },
  error: function() {
    alert("BAD");
  }
});  
share|improve this answer
    
It Worked!!!!!Thank You Very Much!!!!! –  user1887339 2 days ago
2  
Default type is GET in jquery ajax So it does not matter if 'GET' is not passed in AJAX. I don't think setting type to 'GET' solves the problem. It is removing of 'dataType' which solves the problem –  blunderboy 2 days ago
add comment

Try this

$.ajax({
  url: "n1.php",
  dataType: 'json',
  success: function(json_data){
    var data_array = json_data; // Do not parse json_data because dataType is 'json'
    var rec = data_array[0];
    alert("GOOD");
  },
  error: function() {
    alert("BAD");
  }
});

Now, two things to note here:

  1. You have not passed HTTP Method in the ajax but by default it is GET as mentioned here in jQuery AJAX docs. Please pass appropriate method type if it is not GET.

  2. Since you have sent dataType as 'json', you need not parse json received in the response in success handler.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.