0

I am trying to get the JSON response in success AJAX from MySql.
This is the Error i am getting in my console -> Uncaught SyntaxError: Unexpected token <

My AJAX Code.

    function getMessage(siteid)
    {   
    document.getElementById("add").disabled = true;
    $.ajax({
        type: "POST",               
        url: "checkSite.php",   
        data: { siteid : siteid }, //6 digit number
        success: function(data) {
            alert(data);
            var obj = jQuery.parseJSON(data);
            if(objData.status == "not available"){
                alert("Not Available");
                document.getElementById("add").disabled = false;
            }
            if(objData.status == "available"){
                alert("Available");
                document.getElementById("add").disabled = false;
            }
        },
        error: function(result){
            alert("error");
        }
    }); 
}

My checkSite.php

$siteid = $_POST['siteid']; 
if($siteid !=''){
   $sql = "SELECT * FROM  tablename WHERE siteid='$siteid'";
   $result2 = mysql_query($sql);
   if(mysql_num_rows($result2) > 0){        
       echo json_encode(array('status' => 'not available'));
   }
   else{            
    echo json_encode(array('status' => 'available'));
   }        

}

Its showing me parse Error. jQuery.parseJSON(data) is not at all working.

4 Answers 4

2

There is a syntax error on this line

data: { siteid : siteid }

add a comma at the end

data: { siteid : siteid },
                         ^
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. but i have missed it here.. But my error is in success.
No Anish.. i have issues in Success AJAX.
This is my colsole.log(data) <!DOCTYPE html> <html> <head> </head> {"status":"available"}
you are not returning a valid json, it has a doctype also with the json, so invalid json. so what you can do just either remove the doctype or move your php code to a php file not on a php webpage. I prefer the latter.
Jai, Please suggest me how to do the same. Thanks
|
1

try something like this and console.log and check the result

$.ajax({            

    url         :   'checkSite.php',
    dataType    :   'json',
    cache       :   false,
    contentType :   false,
    processData :   false,
    data        :   { siteid : siteid },                           
    type        :   'post',
    success     :   function(data){ 
                    console.log (data);
   }
});

Comments

0

Try this

$.ajax({
                type: "POST",               
                url: "checkSite.php",   
                data: { siteid : siteid }, //6 digit number
                dataType : 'json'
                success: function(data) {
                    if(data.status == "not available"){
                        alert("Not Available");
                        document.getElementById("add").disabled = false;
                    }
                    if(data.status == "available"){
                        alert("Available");
                        document.getElementById("add").disabled = false;
                    }
                },
                error: function(result){
                    alert("error");
                }
            }); 
        })

Comments

0

You did not have else block for "if($siteid !='')" case, so you will get empty from this service call which can't be parse by JSON in success callback. for your case of exception this if condition"if($siteid !='')" seemed to be failing.

So, you checksite.php should be like this shown below.

$siteid = $_POST['siteid']; 
if($siteid !=''){
  if(siteid =='1'){        
       echo json_encode(array('status' => 'not available'));
   }
   else{            
    echo json_encode(array('status' => 'available'));
   }        

}
else
{
     echo json_encode(array('status' => 'not a valid site id'));
}

EDIT:

This can be a content type issue also, use dataType as json, and you don't require to parse json explicitly, you can skip the line "jQuery.parseJSON(data);"

for your case its taking html I believe, its might add html/header tags so JSON.parse could not be parsed. see below, your modified code.

$.ajax({
    type: "POST",               
    url: "checkSite.php",   
    data: { siteid : siteid }, //6 digit number
   dataType : 'json',
   accepts: "application/json; charset=utf-8",
   success: function(data) {
        console.log(data);
        var objData= data;
        if(objData.status == "not available"){
            alert("Not Available");
            document.getElementById("add").disabled = false;
        }
        if(objData.status == "available"){
            alert("Available");
            document.getElementById("add").disabled = false;
        }
    },
    error: function(result){
        alert("error");
    }
});

Hope this will solve your problem!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.