0

I am getting an

Uncaught SyntaxError: Unexpected token ( 

error on the following ajax code. The error is on this line:

function(resultArray) {

The code is:

$(document).ready(function() {

displaycars();
function displaycars () {
    $.ajax({
            type : 'GET',
            url : '/modules/crm/selector-ajax.php',
            data : {
                'action' : 'get-images'
            },
            success : function(result) {

                var selectorDiv = $('#car_isotope_gallery');

            var resultArray = $.parseJSON(result);


            console.log(result);
            console.log(selectorDiv);
            console.log(divHtml);
            console.log(resultArray);

            function(resultArray) {
              var divHtml = '';
              var i;
              for (i = 0; i < arr.length; i++) {
                  divHtml +=  "<div class='element-item honda parts plans class'><h3 class='name'>" + resultArray[i].product_name + "</h3><p class='weight'><img src='" + resultArray[i].photo + "' width='80' border='0' alt=''></p></div>"; 
              }
            }
        console.log(divHtml);
        selectorDiv.html(divHtml);


        },
        error : function(err) {
            console.log('ajax failure');
        }
    });
  }
}); // end of document ready

I'm just not seeing the issue and have been hitting my head against the wall for way too long now. Any help is gratefully appreciated.

Frank
  • 89
  • 2
  • 11

2 Answers2

2

At the location where you have function(resultArray) {, the parser is expecting to see a statement (which can be a statement like for, if, etc., or any expression, because expressions are valid statements in JavaScript). Because it's expecting a statement, when it sees function, it expects that to be a function declaration, not a function expression. Function declarations must have function names, so the unexpected ( is the one after function.

I don't know what you're trying to do with that function, nothing ever calls it (that's part of the problem), but you'll need to give it a name (and presumably then call it somewhere, using that name), or make the parser expect an expression at that point, as outlined here and here.

Community
  • 1
  • 1
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Taking out the line function(resultArray) and the corresponding {} got rid of the syntax error. Now I've hit the answer below which is the undefined arr. Working on that now. Thank you so much for your help and your excellent explanation. – Frank Mar 26 '15 at 16:13
  • Changing arr to resultArray.length fixed the last issue. Its working now. – Frank Mar 26 '15 at 16:15
-2

To start with, variable arr is undefined. May be you meant resultArray?

gowthamnvv
  • 229
  • 2
  • 4
  • It was supposed to be resultArray. Between your suggestion and the one above, it is working now. THANK YOU!!!! – Frank Mar 26 '15 at 16:15