简体   繁体   中英

Using dataType: “json” does not run AJAX code

I have been searching for hours and haven't found any solutions for my problem.
This is my jQuery/Ajax code:

$(document).ready(function() {  
  $(".submit_btn").click(function () { 
      var name = $("input#name").val();  
      var dataString = 'query='+$("#query").val()+'&facebook='+facebook+'&twitter='+twitter;
      $.ajax({  
          type: "POST", 
          url: "selectDataSets.php",
          dataType: "json",
          data: dataString,  
          success: function(data) { 

            alert ("hello");

          }  
        });  
        return false;  
  });  
});

Now my selectDataSets.php code:

<?
include_once 'config.php';
if ((isset($_POST['facebook'])||isset($_POST['twitter']))&&isset($_POST['query'])) { 

$elements=0;
$dataSet=array();
$tt=array();
$fb=array();

mysql_connect($config['database_url'], $config['database_user'], $config['database_password']) or die(mysql_error());
$queries = explode(";", $_POST['query']);

foreach ($queries as $query){
    if(isset($_POST['facebook']) && $_POST['facebook'] == true){

        mysql_select_db("facebook") or die(mysql_error());
        $mysql_query = "SELECT * FROM page WHERE lower(name) LIKE '%".mysql_real_escape_string(strtolower($query))."%'";

        // Perform Query
        $result = mysql_query($mysql_query);

        while ($row = mysql_fetch_assoc($result)) {
            $set = array(
                    "name" => str_replace("-","",$row['name']),
                    "likes" => $row['likes'],
                    "about"   => $row['talkAbout'],
                    "source"   => "Facebook (Page)",
                    "id"   => $row["id"],
                    "query"   => $query
            );
            $fb[$elements]=$set;
            $elements++;
        }
        mysql_free_result($result);

    }
 }

mysql_close();
echo json_encode($fb);
}

With dataType: "json" , the alert("Hello") does not work, as well as nothing that I add inside the success callback. Although when I remove dataType: "json" , the alert works, but the variable data is not recognized as JSON (as I keep getting undefined when I try to do data[0].name ), even though I have checked and data is on the format [ { "name: ... } ] , which I guess is correct JSON. I don't know what else to do, as I have a similar (almost the same) code on another php file that works perfectly with the dataType: "json" .

Thanks in advance!

Can you see the actual output of the PHP script/AJAX call using Chrome's network window?

Make sure there are no MySQL errors and stuff. The reason you get no error when you don't put dataType: "json" is because the JSON parser is not attempting to read your malformed JSON. For some reason, your example JSON is good but whatever AJAX is receiving is not good. From the network window, select your AJAX call and look at the response tab. This will show you the real output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM