1

In my console I keep getting a syntax error on line 10. I cannot figure out what is wrong with my code. Thank you so much for the help.

$(document).ready(function () { 
    // Skill sort on change 
    $('#order_by').on('change', function(){ 
        $.ajax({ 
            type: "POST", 
            url: "sort_skill_be.php", 
            dataType: "json", 
            data: { skill: this.value }, 
            error: function (qXHR, textStatus, errorThrown) { 
                console.log(errorThrown);  // SYNTAX ERROR HERE
            }, 
            success: function (result) { 
                alert(result[0].skill); 
                alert('i got here'); 
            } 
        }); 
    }); 
});

ERROR MSG SyntaxError {}

Thank you so much for your help and assistance.

When I alert the errorThrown I get SyntaxError: Unexpected end of input

EDIT SERVER SIDE CODE

<?php
session_start();
include 'database.php';
$skill_sort = $_POST['skill'];
sortSkill($skill_sort);
?>


function sortSkill($skill){
    // If All skill is selected display posts normally
    if ($skill == 'All'){
        displayPosts();
        exit;
    }
    $db = connect();
    $sql = "SELECT * FROM users LEFT JOIN posts 
            ON users.idUsers = posts.fkuser 
            WHERE posts.fkuser IS NOT NULL and users.skill=:skill
            ORDER BY date DESC";
    $stmt = $db->prepare($sql);
    $stmt->execute(array(':skill' => $skill));      
    if ($stmt->rowCount() == 0){
        // Nothing has returned
        unset($_SESSION['recent_posts']); // Reset session of posts if no posts appear.
    } 
    else
    {
        $recent_posts = helpFetchPostInfo($stmt);
        return $recent_posts;
    }
}




 // IS THIS CORRECT??
function helpFetchPostInfo($stmt){
    $results = $stmt->fetchAll();
    $recent_posts = array();
    foreach ($results as $row){
        $post = array(
            'username' => $row['username'],
            'steam' => $row['steam'],
            'skill' => $row['skill'],
            'description' => $row['description'],
            'date' => $row['date'],
        );
        array_push($recent_posts, $post);
    }
    return json_encode($recent_posts); 
}

?>

Headers / Network from browser

Request URL:http://zeex/sort_skill_be.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:10
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:PHPSESSID=9db38628119ef72c7be1c20eaa5da13d
Host:zeex
Origin:http://zeex
Referer:http://zeex/index.php
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
skill:High
Response Headersview source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Fri, 21 Jun 2013 01:40:15 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=97
Pragma:no-cache
Server:Apache
X-Powered-By:PHP/5.4.4
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
AustinT
  • 1,998
  • 8
  • 40
  • 63
  • What does the response look like? Show us the source which the server sent. – Bergi Jun 21 '13 at 01:08
  • Open chrome dev tools, "Sources" tab, turn on "break on the unhandled exeption" mode, refresh the page and stop guessing – zerkms Jun 21 '13 at 01:11
  • How exactly do I turn on break on the unhandled exception – AustinT Jun 21 '13 at 01:13
  • I don't see a syntax error in your js, I think you have another problem and it might be on the server side, why don't you try to catch the variable skill the normal way $_post['skill'] ? – CME64 Jun 21 '13 at 01:14
  • @CME64 Sorry I do not know exactly what you mean. – AustinT Jun 21 '13 at 01:15
  • Check this http://stackoverflow.com/questions/3594923/chrome-uncaught-syntaxerror-unexpected-end-of-input – Sushanth -- Jun 21 '13 at 01:16
  • here is your code on jsbin it is working fine : http://jsbin.com/uyonit/1/edit (the error message is due to the origin of the ajax) – CME64 Jun 21 '13 at 01:16
  • what kind of server are you using ? it looks like php,, apache ? – CME64 Jun 21 '13 at 01:17
  • I am using php apache. – AustinT Jun 21 '13 at 01:17
  • yes, the sent query can be received on the server side usign $vriable = $_POST["skill"]; check this out http://www.w3schools.com/php/php_post.asp – CME64 Jun 21 '13 at 01:19
  • @Sushanth-- is right, when I remove the datatype:json the error goes away and I get another error saying it does not recognize skill `Uncaught TypeError: Cannot read property 'skill' of undefined ` – AustinT Jun 21 '13 at 01:19
  • first debug the reception on the server side if you receive that variable that means your processing for information is not right on the server .. try echoing it – CME64 Jun 21 '13 at 01:20
  • Looks like your response is not a valid `json` string.. try pasting it in jslint and see if it is valid – Sushanth -- Jun 21 '13 at 01:20
  • @CME64 - just FYI, people on this site like to flame for referencing w3schools. You would likely have suffered an assault of downvotes if that was in an answer – Kai Qing Jun 21 '13 at 01:21
  • Ah.. so I cannot pass an array of an array to JSON? im guessing. – AustinT Jun 21 '13 at 01:21
  • I'm trying to help you here, if you don't want the help .. I have no problem .. and check it out never down-voted ! – CME64 Jun 21 '13 at 01:22
  • @CME64 don't worry your helping, thanks for your time. But now I feel it has something to do with me incorrectly using JSON object – AustinT Jun 21 '13 at 01:24
  • what do you get when you try to print the $skill ? – CME64 Jun 21 '13 at 01:31
  • In PHP I do get the correct skill, but in jquery it is not allowing me to access it. – AustinT Jun 21 '13 at 01:32
  • I am sure it has to do with something right here `$results = $stmt->fetchAll(); $recent_posts = array(); foreach ($results as $row){ $post = array( 'username' => $row['username'], 'steam' => $row['steam'], 'skill' => $row['skill'], 'description' => $row['description'], 'date' => $row['date'], ); array_push($recent_posts, $post); } return json_encode($recent_posts);` – AustinT Jun 21 '13 at 01:33
  • 1
    Thanks for the code, though I can't see where you are `echo`ing anything. However, the serverside code is not that interesting, please post *what the browser received* instead. You can view it in the network tab of the devtools. – Bergi Jun 21 '13 at 01:36
  • ok I don't know how you planned it, but this is how to use json ,, you send your objects normally and on the server-side it will be received as the original object, the server then responds with an object (array,string .. ) then the dataType: 'json' will convert your response into json objects .. default uses object d – CME64 Jun 21 '13 at 01:38
  • also it will be interesting for you to check out contentType: 'application/json; charset=UTF-8' – CME64 Jun 21 '13 at 01:39
  • @Bergi I added my browser sent information – AustinT Jun 21 '13 at 01:41
  • @CME64 what exactly do you mean / how do I check out the contentType, do I just add it to the ajax code? – AustinT Jun 21 '13 at 01:43
  • yes, it helped me some times, it forces the response to be parsed as json http://api.jquery.com/jQuery.ajax/ ( not w3s) – CME64 Jun 21 '13 at 01:45
  • Thanks. Well, in the first request, `skill:High` is not valid JSON, and the second one has `Content-Length:0`. So where does your PHP script actually output anything? – Bergi Jun 21 '13 at 01:46
  • Thanks I added it, but still getting the same problem. – AustinT Jun 21 '13 at 01:47
  • Is my PHP suppose to output something? I thought its suppose to store as a JSON object as I would be able to use the data in jquery from the php. – AustinT Jun 21 '13 at 01:48
  • @Bergi is there a better way for me to store the information and access it? I have made another thread to try to figure out if I am doing this ajax stuff correctly I am still new at it. http://stackoverflow.com/questions/8823925/how-to-return-an-array-from-an-ajax-call – AustinT Jun 21 '13 at 01:49
  • (First, sorry, I didn't notice that `skill:High` is the *request* body. It's fine. The empty answer is not) Of course the PHP is supposed to output something. It should send the requested data to the browser, that's what you asked it to do (didn't you?). Your `success` callback even *expects* some data back (an array with an object inside). Of course the server might store something in the DB and do anything else, but the primary purpose of your request seems to be to *get data back*. – Bergi Jun 21 '13 at 02:00
  • 2
    You PHP needs to `echo` the result of `sortSkill(...)`. – Vedran Šego Jun 21 '13 at 02:00
  • 2
    (+1 to @Vedran) …and set the `Content-Type` response header to `application/json` – Bergi Jun 21 '13 at 02:01

0 Answers0