2014-08-29 1 views
0

안녕하세요 저는 JavaScript가 처음인데 Tim Wright의 "Learning Javascript"를 통해 작업하고 있습니다. JSON.parse를 사용하여 문자열에서 변환하려고하지만 코드를 실행할 때 오류가 발생합니다. Uncaught SyntaxError : 26 번째 줄에서 예기치 않은 토큰}을 볼 수 있지만 26 번째 줄에서}를 볼 수 없습니다. 복사 및 붙여 넣기를 시도했습니다. 내가 뭔가 잘못했는지 알아보기 위해 pdf의 코드가 있지만 여전히 같은 오류가 발생합니다.예기치 않은 토큰} 26 행에 있지만 아무 것도 없습니다. 26 ajax call

//create an instance of the xmlhttrequest 

function getHTTPObject() { 

    //initialize the variable 
    var xhr; 

    if (window.XMLHttpRequest) { 
     xhr= new XMLHttpRequest(); 
    }else if (window.ActiveXObject) { 
     xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
    return xhr; 
}//end getHTTPObject 

function ajaxCall(dataUrl, callback) { 

    var request = getHTTPObject(); 


    request.onreadystatechange = function() { 
     if (request.readyState === 4 && request.status === 200) { 

      var contacts = JSON.parse(request.responseText); 

      if(typeof callback === "function") { 
       callback(contacts); 
      }//end function check 
     }//end ajax status check 

    } //end onreadystatechange 
    request.open("GET", dataUrl, true);  
    request.send(null); 
} 




ajaxCall('data/contacts.json'); 

나는 분명히 뭔가 빠져 있지만 아마도 나는 그것을 볼 수 없다는 것을 알고있다. 도와 주셔서 감사합니다. 당신은 JS의 trailling 쉼표를 가질 수 있지만

{ 
    "name": "Mum", 
    "email":"[email protected]", 
}       ^-- remove this comma, and corresponding commas in all the other "objects". 

:

{ "addressBook" : [ 


    { 
     "name": "Elysha", 
     "email":"[email protected]", 
    }, 

    { 
     "name": "Luke", 
     "email":"[email protected]", 
    }, 

    { 
     "name": "Dad", 
     "email":"[email protected]", 
    }, 

    { 
     "name": "Mum", 
     "email":"[email protected]", 
    } 



] 

}

+1

어떤 줄이 26 번인지 알려주십시오. – Barmar

+0

어떤 줄? :-) –

+1

@ user2609980 : 아마도'request.responseText'라고합니다. –

답변

1

는 JSON 문자열에 콤마가있다 :

업데이트는 여기처럼 내 JSON 파일이 어떻게 표시되는지를 보여줍니다 객체 리터럴 인 경우 JSON 구문에서 허용되지 않습니다.

+0

감사합니다. 그냥 쉼표가 아닌 모든 항목에 쉼표를 추가했습니다. – user3546457