2013-11-27 2 views
0

도움이 필요합니다! Codeigniter 서버에서 json 데이터를 가져 오려고합니다. 난 JS 에러html 로컬 파일의 jquery로 json 데이터 가져 오기

와 JSON 데이터 응답을 구하는 본

public function index() 
{ 
    $type_message = 'success'; 
    $message = 'Mobile plateform'; 
    $this->output->set_header("Access-Control-Allow-Origin: *"); 
    $this->output->set_header("Access-Control-Expose-Headers: Access-Control-Allow-Origin"); 
    $this->output->set_status_header(200); 
    $this->output->set_content_type('application/json'); 
    $this->output->_display(); 
    echo json_encode(array('type_message' => $type_message, 'message' => $message)); 
} 

$.ajax({ 
    type: "POST", 
    url : serverUrl+'mobcontroller/users/', 
    crossDomain: true, 
    async: false, 
    data : $("#formlogin").serialize(), 
    dataType : MobileJsonType[1], 
    beforeSend : function(){//do stuff here}, 
    success : function(responseData) { 
     alert(JSON.stringify(responseData)); 
    }, 
    error : function(jqXHR, textStatus, errorThrown) { 
     // do stuff here  
    }, 
    complete: function(xhr, settings){ 
     alert(JSON.stringify(xhr)+'\n'+settings); 
    } 
}); 

이그나이터 제어기 보면 : 이 내 HTML 로컬 파일 (//myproject/index.html d)이며

Uncaught SyntaxError: Unexpected token : 

도와주세요!

+0

시도해보십시오. 성공 : function (responseData) {alert (responseData.type_message);}. 가정 MobileJsonType [1] 값'json' –

답변

0

success: function(data) { 
    console.log(data) 
} 

에 성공 기능을 변경하고 우리가 출력이 무엇인지 알려

.

저는 codeigniter에 익숙하지 않지만 $ this-> output -> _ display(); JS가 구문 분석하려고하는 것의 에코? 당신은 위의

ob_clean(); 

을 배치 할 경우

echo json_encode(... 

는 JS로 다시 보내려고 버퍼, 그리고 JS 구문 분석 할 수있는 돌아갈 것입니다 그런 식으로 만 인코딩 된 JSON을 취소합니다 .

0

아약스 요청은 다음과 같아야합니다.

$.ajax({ 
    type: "POST", 
    url : serverUrl+'mobcontroller/users/', 
    crossDomain: true, 
    async: false, 
    data : $("#formlogin").serialize(), 
    beforeSend : function(){//do stuff here}, 
    success : function(responseData) { 
     alert(JSON.stringify(responseData)); 
    }, 
    error : function(jqXHR, textStatus, errorThrown) { 
     // do stuff here  
    }, 
    complete: function(xhr, settings){ 
     alert(JSON.stringify(xhr)+'\n'+settings); 
    } 
}); 

내가 dataType : MobileJsonType[1]
은 또한 당신이 JSON.stringify을 사용할 필요가없는 propertydata 제거했습니다. 반환 된 결과로 인해 이미 JSON입니다 PHP의 json_encode

그리고 첫 번째 요청이 허용되거나되지 않은 경우 확인하기 위해 서버 안타 인해 beforeSendbeforeSend 때문에 당신이 직면하고있는 오류입니다. true를 보내면 실제 요청이 진행되고 그렇지 않으면 직접 처리해야합니다. 직렬화 된 데이터로 토큰을 보내야한다고 가정합니다. 희망이 도움이됩니다. 당신이 재산을 제거하면 beforeSend 요청을 잘 작동합니다 생각합니다.