json Uncaught SyntaxError : 예기치 않은 토큰 :
전화를 걸어 매우 간단한 한 줄의 JSON 파일을 검색하려고합니다.
$(document).ready(function() {
jQuery.ajax({
type: 'GET',
url: 'http://wncrunners.com/admin/colors.json' ,
dataType: 'jsonp',
success: function(data) {
alert('success');
}
});
});//end document.ready
RAW 요청은 다음과 같습니다.
GET http://wncrunners.com/admin/colors.json?callback=jQuery16406345664265099913_1319854793396&_=1319854793399 HTTP/1.1
Host: wncrunners.com
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2
Accept: */*
Referer: http://localhost:8888/jquery/Test.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
다음은 RAW 응답입니다.
HTTP/1.1 200 OK
Date: Sat, 29 Oct 2011 02:21:24 GMT
Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d SE/0.5.3
Last-Modified: Fri, 28 Oct 2011 17:48:47 GMT
ETag: "166a2402-10-4eaaeaff"
Accept-Ranges: bytes
Content-Length: 16
Content-Type: text/plain
Connection: close
{"red" : "#f00"}
JSON이 응답 (빨간색 : # f00)으로 돌아 오지만 Chrome은 Uncaught SyntaxError : Unexpected token : colors.json : 1을보고합니다.
URL 자체로 직접 이동하면 JSON이 반환되고 브라우저에 표시됩니다.
colors.json의 내용을 JSLINT에 붙여 넣으면 json이 유효성을 검사합니다.
이 오류가 발생하지 않고 성공 콜백에 도달하지 못하는 이유에 대한 아이디어가 있습니까?
편집 -위의 jQuery.ajax () 호출은 jsfiddle.net에서 완벽하게 실행되며 예상대로 '성공'경고를 반환합니다.
편집 2- 이 URL은 'http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json'이 잘 작동합니다. 예상치 못한 토큰. 여러 다른 URL을 테스트했으며 예기치 않은 토큰을 던지지 않는 유일한 URL은 TYPE : text / javascript로 반환되는 wunderground입니다.
text / plain 및 application / json으로 반환 된 스트림이 올바르게 구문 분석되지 않습니다.
jQuery에 JSONP 응답 을 기대하도록 지시했으며 , 이것이 jQuery가 callback=jQuery16406345664265099913_1319854793396&_=1319854793399
URL에 부분을 추가 한 이유입니다 (요청 덤프에서 볼 수 있음).
반환하는 것은 JSONP가 아니라 JSON입니다. 귀하의 응답은 다음과 같습니다.
{"red" : "#f00"}
jQuery는 다음과 같은 것을 기대합니다.
jQuery16406345664265099913_1319854793396({"red" : "#f00"})
If you actually need to use JSONP to get around the same origin policy, then the server serving colors.json
needs to be able to actually return a JSONP response.
If the same origin policy isn't an issue for your application, then you just need to fix the dataType
in your jQuery.ajax
call to be json
instead of jsonp
.
I have spent the last few days trying to figure this out myself. Using the old json dataType gives you cross origin problems, while setting the dataType to jsonp makes the data "unreadable" as explained above. So there are apparently two ways out, the first hasn't worked for me but seems like a potential solution and that I might be doing something wrong. This is explained here [ https://learn.jquery.com/ajax/working-with-jsonp/ ].
The one that worked for me is as follows: 1- download the ajax cross origin plug in [ http://www.ajax-cross-origin.com/ ]. 2- add a script link to it just below the normal jQuery link. 3- add the line "crossOrigin: true," to your ajax function.
Good to go! here is my working code for this:
$.ajax({
crossOrigin: true,
url : "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.86,151.195&radius=5000&type=ATM&keyword=ATM&key=MyKey",
type : "GET",
success:function(data){
console.log(data);
}
})
I had the same problem and the solution was to encapsulate the json inside this function
jsonp(
.... your json ...
)
That hex might need to be wrapped in quotes and made into a string. Javascript might not like the # character
참고URL : https://stackoverflow.com/questions/7936610/json-uncaught-syntaxerror-unexpected-token
'Program Tip' 카테고리의 다른 글
Python 속성 확인을위한 get () 유사 메소드 (0) | 2020.11.04 |
---|---|
Zend 자습서로 시작-Zend_DB_Adapter에서 예외 발생 : "SQLSTATE [HY000] [2002] 해당 파일 또는 디렉터리가 없습니다." (0) | 2020.11.04 |
“미해결 포함 : (0) | 2020.11.04 |
CSV 파일을 Pandas DataFrame으로 가져 오기 (0) | 2020.11.04 |
이 경우 bool과 not bool이 모두 true를 반환하는 이유는 무엇입니까? (0) | 2020.11.04 |