I hope there is no problem asking. I am modifying a
previously
working project. I upgraded it from mysql to mysqli and testing it on
Ubuntu instead of Windows.
That being said, my ajax functions don't seem to work, although
my json data is in proper format. According to the error callback, the
content type is text/html
I have tried the following:
- changing contentType in ajax call to "application/json"
- viewed the response text, which was returning the correct thing.
- changing the content type in the php header function but that
didn't help
I have read on stackoverflow that sometimes the response comes
wrapped in <html> tags...but I couldn't confirm that in my script.
- <script>
-
//alert("test");
-
$(document).ready(function(){
-
loadResults();
-
function loadResults(){
-
//alert('<?php echo urlencode($q)?>');
-
$.ajax({
-
url: 'fetch/getsearch.php?q=<?php echo urlencode($q)?>',
-
dataType: 'json', success: function(data)
-
{
-
//console.log(data);
-
$('#users-results').html("");
-
$('#tags-results').html("");
-
var users = data[0].users;
-
var tags = data[0].tags;
-
-
if(!users[0].none){
-
for (var i = 0; i<users.length; i++){
-
$('#users-results').append("<a href='#'
class='users' id='u_"+i+"'>"+users[i].user+"</a><br>");
-
}
-
}else{
-
$('#users-results').append("<span class='users'>"+users[0].none+"</span>");
-
}
-
-
if(!tags[0].none){
-
for (var i = 0; i<tags.length; i++){
-
$('#tags-results').append
-
("<a
href='room.php?cat="+tags[i].cat+"&tag="+tags[i].tag.substr(1,tags[i].tag.length)+"'
class='tags' id='t_"+i+"'>"+tags[i].tag+"</a><span>"+tags[i].cat+"</span><br>");
-
}
-
}else{
-
$('#tags-results').append("<span class='tags'>"+tags[0].none+"</span>");
-
}
-
-
$(".users, .tags").css('padding-left','15px');
-
$(".tags").css('padding-right','15px');
-
$(".users, .tags").css('font-size','16px');
-
-
},
-
error: function (xmlHttpRequest, textStatus,
errorThrown) {
-
console.log(xmlHttpRequest.getAllResponseHeaders());
-
alert(errorThrown);
-
}
-
-
});
-
}
- </script>
And
my php:
- <?php
-
- require_once '../config/db.php';
- session_start();
-
- if
(!isset($_GET['q']) || trim($_GET['q']) == "")
-
header('Location: ../alert/error.php');
- else {
-
$q = strtolower(urldecode($_GET['q']));
-
$q = str_replace("'", "''", $q);
- }
-
- $results
= array();
- $users_array
= array();
- $tags_array
= array();
-
- $user_query
= mysqli_query($con, "SELECT * FROM user_table WHERE
`username` LIKE '%" . $q . "%' ORDER BY username");
- $tag_query
= mysqli_query($con, "SELECT * FROM tag_table WHERE `tagname`
LIKE '%" . $q . "%' ORDER BY tagname");
- if
(mysqli_num_rows($user_query) > 0) {
-
while ($user = mysqli_fetch_array($user_query, MYSQLI_BOTH)) {
-
$users_array[] = array('user' => $user['username']);
-
}
- }
else {
-
$users_array[] = array("none" => "No users
were found.");
- }
- if
(mysqli_num_rows($tag_query) > 0) {
-
while ($tag = mysqli_fetch_array($tag_query, MYSQLI_BOTH)) {
-
$tags_array[] = array('tag' => $tag['tagname'],
-
'cat' => $tag['category']);
-
}
- }
else {
-
$tags_array[] = array("none" => "No tags were found.");
- }
-
- $results[]
= array('users' => $users_array,
-
'tags' => $tags_array);
- // header("content-type:application/json");
- echo json_encode($results);
- mysqli_close($con);
- ?>
If anyone can tell me how to tackle this issue, it would be perfect.
Thanks.