2017-10-28 11 views
0

추가 데이터를 내 REST 서버에 전달하는 것이 잘 작동하지 않습니다.AJAX를 통한 데이터 전달이 NULL 값으로 응답합니다.

// Client 
$(document).on('click', '#car', function(e){ 
    e.preventDefault(); 
    var name = $(this).data('name'); // Tesla 
    var model = $(this).data('model'); // X 
    $.ajax({ 
     type: "GET", 
     contentType: 'text', 
     cache: false, 
     url: "http://server:80/api/v1/cars/" + name, 
     data: JSON.stringify({ 
       "model": model 
       }),   
     success: function(data, textStatus, jqXHR){ }, 
     error: function(jqXHR, textStatus, errorThrown){}      
    }); 
}); 

문제는 car가 잘 작동되는 반면 model 내 서버에 항상 NULL 있다는 것이다 :

나는이 간단한 아약스 호출을 가지고있다. 또한 var_dumped 전체 요청을 검색하고 model을 검색했지만 불행히도 거기에 없습니다. 내 요청 URL이 브라우저에서 developement에 도구에서 다음과 같습니다 이유

// Server 
public function show($request, $response, $args) 
{   
    $model = $request->getParsedBody()['model']; 
    $name = $args['id']; 

    echo $model;  // NULL 
    echo $name;  // Tesla 
} 

는 또한 이해가 안 :

http://server:80/api/v1/cars/Tesla.txt?{"model":"X"} 

그는 요청 URL의 끝에 JSON을두고 나는이 생각 표준 행동이 아닌가?

+0

전 당신은 TYPe에서 데이터를 전달할 수 없다고 생각한다 = try try POST –

+0

@ReubenGomes 형식을 GET에서 POST로 바꾸면 작동하지만, 왜 그런지 궁금하다. 불행히도, 공식 문서는 그것에 관해 명확하게 명확하지 않습니다. 당신은 작은 게시물을 만들 수 있으며 귀하의 답변을 수락합니다. 고맙습니다. – Magiranu

+0

마기 라누 아래를 확인하십시오 –

답변

1

일반적으로 얻을 수있는 쿼리 문자열 경우 데이터 URL에 변수가 추가 된 URL을 통과하고 POST가 요청 본문을 통해 전달됩니다 POST는 더 많은 데이터를 전달할 수 있습니다. GET 모든 것을 귀하의 모든 데이터가

Your typical query String : http://example.com/over/there?name=ferret&sname=somethingelse 

Yout would use $_GET['name']; 

and For POST 

URL:http://example.com/over/there 
Message Body name=ferret&sname=somethingelse 

$_POST['name']; 

을 이렇게 먹을수록 기본으로 메시지 본문

throught를 보낼 수 있습니다 더 이상 데이터가 POST에 추가 할 수있는 URL의 일부가되고있다 PHP에서 당신이 뭔가하면 자동으로 이 $_REQUEST 손잡이 모두를 처리 할 request.getParameters을 사용 jave에서 $_REQUEST; 라고해야 얻을 POST 데이터

1

JSON.stringify({"model": model}) 대신 data:{model:model}으로 시도하십시오. http://api.jquery.com/jquery.ajax/

+0

운 좋게 노력했지만. Nouphal을 입력 해 주셔서 감사합니다. – Magiranu

0

이 방식으로 데이터를 전달 자세한 내용은 참조 데이터 : {모델 : JSON.stringify (모델)},

$(document).on('click', '#car', function(e){ 
    e.preventDefault(); 
    var name = $(this).data('name'); // Tesla 
    var model = $(this).data('model'); // X 
    model = JSON.stringify(model); 

    $.ajax({ 
     type: "GET", 
     contentType: 'text', 
     cache: false, 
     url: "http://server:80/api/v1/cars/" + name, 
     data: {model:model},   
     success: function(data, textStatus, jqXHR){ }, 
     error: function(jqXHR, textStatus, errorThrown){}      
    }); 
}); 
+0

내 잘못 내가 잘못된 장소에 편집을 넣어 내 대답은 의견을 잊어 버렸습니다. –