2013-05-20 2 views
1
Error - Error: Uncaught SyntaxError: Unexpected token : 

exact issue error
플랫폼 - Icenium.검도 UI - 오류 : catch되지 않은 구문 에러 : 예기치 않은 토큰 :

우리는 데이터를 얻을 원격 서비스 - http://localhost:35798/RestServiceImpl.svc/json을 사용하고 있습니다. 필자는 서비스에서받은 데이터 형식을 첨부했습니다. return format of data

여기 내 코드입니다 :

var dataSource = new kendo.data.DataSource({ 
    schema: { 
     data: "d" 
    }, 
    transport: { 
     read: { 
      url: "http://localhost:35798/RestServiceImpl.svc/json", 
      dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests 
      data: { 
       id: "4" 
      }, 
      type: "GET", 
      contentType: "application/json;charset=utf-8" 
     }, 
     change: function() { 
      alert('called'); 
      var data = this.data(); 
      console.log(data.length); // displays "77" 
      debugger; 
      $('#txtJson').val(data[0].name); 
     } 

    } 
}); 

$("#submitButton").click(function() { 
    dataSource.read(); 
    var data = dataSource.data(); 
    console.log(data.length); 
}); 

여기 내 서비스 코드입니다 -

[OperationContract] 
    [WebInvoke(Method = "GET", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "json?id={id}") 
     ] 
    List<Person> JSONData(string id); 

Q 1) 방법이 오류를 해결하기 위해 - Uncaught SyntaxError: Unexpected token을, 내가 놓친 게 뭔가?

Q 2) 버튼을 클릭 한 후 dataSource.data().length이 0이되고 난 후에 dataSource.read()을 호출하고 있습니다. 이것은 dataSource.bind(change:function())에서 처리해야한다고 생각합니다. 그러나 이후 dataSource.read() 변경 기능이 실행되지 않습니다.

+0

당신은 이미지를 포함하지 않았다! – OnaBai

+0

이미지를 추가했습니다. 내가 원하는 것을 가지고 당신이 분명하기를 바랍니다. 또한 이미지에서 서비스에서 반환되는 데이터를 지정했습니다. 감사합니다, –

+0

JSONP가 아니라 JSON을 반환하고 있다는 것을 알고 계셨습니까? 그것이 "예기치 않은 토큰"오류의 원인입니다. – OnaBai

답변

1

JSON과 JSONP의 문제를 해결하면 datasource.schema에 실제로 데이터가 수신 된 JSON의 요소 인 안에 있다고 표시하지 않으므로 데이터가 여전히 비어 있습니다.

schema은 다음과 같아야합니다

schema: { 
    data: "JSONDataResult" 
}, 

당신은 당신이 무엇을 얻을 디버깅을 위해 스키마에 구문 분석 기능을 추가 할 수 있습니다

schema : { 
    parse: function(response) { 
     console.log("parse"); 
     console.log(JSON.stringify(response, null, 4)); 
     debugger; 
     return response.JSONDataResult; 
    } 
} 
+0

이미 스키마를 사용해 보았지만 행운은 없습니다. 길이는 정의되지 않았습니다. dataSource._pristine 속성 아래에서 데이터를 보았습니다. 여기서 문제가 될 수있는 것은 무엇입니까? http://imageshack.us/f/826/lengthundefined.png/ –

+0

수신 한 내용을 보여주는'schema.parse' 함수를 정의 해보십시오. 내 편집을 참조하십시오. – OnaBai

+0

완벽하게 잘 작동했습니다. 고맙습니다 :). , dataSource.read(), schema parse 함수가 실행 된 다음 stringify 된 다음 dataSource.data()에 데이터가 있습니다. 마지막 질문 하나 - dataSource.read() 이후에 데이터 소스의 변경 이벤트가 시작되지 않는 이유는 무엇입니까? –

관련 문제