0
http://www.serveraddress.com/api/users/1
이 유효하며 값이 올바른 JSON 형식입니다.REST API에 대한 GET 요청
json으로는 :
{
"id":1,
"name":"Ryan Chenkie",
"email":"[email protected]",
"battles_won":0,
"created_at":"2017-02-25 19:20:58",
"updated_at":"2017-02-25 19:20:58",
"blobs":[
{
"id":1,
"name":"Blob 1",
"type":"type A",
"color":"red",
"alive":1,
"level":1,
"exercise_level":-302,
"cleanliness_level":-302,
"health_level":-302,
"owner_id":1,
"created_at":"2017-02-25 19:20:58",
"updated_at":"2017-02-26 01:23:05"
},
{
"id":5,
"name":"Blob 5",
"type":"type C",
"color":"blue",
"alive":1,
"level":1,
"exercise_level":-302,
"cleanliness_level":-302,
"health_level":-302,
"owner_id":1,
"created_at":"2017-02-25 19:20:58",
"updated_at":"2017-02-26 01:23:05"
}
]
}
나는 인 getUser()를 실행하려고
, 나는SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
를 얻을.
내 코드에 어떤 문제가 있습니까? 내 setRequestHeader
이 올바르지 않습니까? 함수가 요구하더라도 전송되기 전에 response
를 (비어있는) 반환
getUser(1);
function getUser(userId) {
var usersUrl = "http://www.serveraddress.com/api/users/";
var params = userId;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", usersUrl + params, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send();
var response = JSON.parse(xhttp.responseText);
return response;
}
결과 JSON 무엇
@Psi OP를 편집하여 JSON을 포함 시켰습니다. 유효한 JSON이라고 말한 JSON 포맷터를 확인했습니다. – falafel
브라우저 도구 또는 Fiddler에서 응답을 확인하십시오. 실제로 JSON을 응답으로 받고 있는지 확인하십시오. – JLRishe