0
다음 두 가지 형태로, 두 서버 쪽 PHP 스크립트가 반환 이 같은 json_encode ($ 결과);JSON 데이터가 다른 데이터 형식으로 반환되는 이유는 무엇입니까?
그래서 클라이언트 측에서 JSON 데이터를 가져와야한다고 가정합니다.
그러나 텍스트가 반환 된 데이터 인 경우 JSON 데이터에 액세스하려면 먼저 텍스트를 변환해야합니다.
사례 II에서 반환 된 데이터는 개체이므로 직접 사용할 수 있습니다.
왜 I과 II가 다른 경우입니까?
는 데이터가 서버에 포장하는 방법을 보지 않고 당신////////////////////////////////////////////////
Case I:
$(document).ready(function() {
var options = {
success: processJson,
dataType: 'json'
};
// bind form using 'ajaxForm'
$('#countyForm').ajaxForm(options);
});
function processJson(data) {
// here data is an object
}
//////////////////////////////////////////////////
Case II:
$(document).ready(function() {
$('#the_button').click(function() {
$.ajax({
type: "GET",
url: "chicken_answer.php",
data: "first=Sean&last=Rowe",
success: function(msg){
// msg is not an object, we have to convert it to an object by calling eval
jsonObj = eval('(' + msg + ')'); // we're getting back JSON text, so we have to convert it to a JavaScript object.
$('#the_answer').html(jsonObj.theAnswer);
}
});
});
});
안녕하세요, Hoerster, 당신 말이 맞습니다. dataType을 추가하면 현재 작동합니다. 감사합니다. – q0987
도와 드리겠습니다. –