2013-05-24 2 views
1

아직 RESTful API 및 JSON에 익숙하지 않으므로 sending JSON to a URIparsing JSON from a request body에 대한 jQuery 및 Play 설명서를 살펴 보았습니다.

불행히도 예기치 않은 동작이 나타납니다.

다음
[info] application - Attempting to download 
[info] application - GET /download?uuid=12512502-5ca4-47bc-a4db-15f1da1979fc 
[info] application - AnyContentAsEmpty 
[info] application - None 
[info] application - Expecting Json data 

는 결과 브라우저 콘솔입니다 :

여기
def downloadResults = Action { request => 
    Logger.info("Attempting to download") 
    Logger.info(request.toString) 
    Logger.info(request.body.toString) 
    Logger.info(request.body.asJson.toString) 
    request.body.asJson.map { json => 
     { 
    Logger.info(json.toString) 
    json.validate[String].map { 
     case uuid => { 
     Logger.info(s"Looking for results-$uuid.txt") 
     Ok.sendFile(new File(s"results-$uuid.txt")) 
     } 
    }.recoverTotal { 
     e => 
     { 
      Logger.info("Detected error: " + JsError.toFlatJson(e)) 
      BadRequest("Detected error: " + JsError.toFlatJson(e)) 
     } 
    } 
     } 
    }.getOrElse { 
     Logger.info("Expecting Json data") 
     BadRequest("Expecting Json data") 
    } 
    } 

이 결과 서버 로그입니다 : 여기

$.ajax({ 
      url: "@routes.Application.downloadResults", 
      data: { 
       uuid: $('body').attr('uuid') 
      }, 
      dataType: 'json', 
      contentType: "application/json; charset=utf-8", 
      success: function(res) { 
       console.log('success') 
      }, 
      error: function(res) { 
       console.log('error') 
      } 
     }) 

내 서버 코드입니다 : 여기

내 AJAX 호출입니다 :

GET http://localhost:9000/download?uuid=12512502-5ca4-47bc-a4db-15f1da1979fc 400 (Bad Request) jquery-1.9.0.min.js:3 
error 

uuid이 URI에 매개 변수로 연결되어있는 것처럼 보입니다.하지만 JSON으로 보내려고했습니다. 지금 당장 어떤 방법으로 문제가 발생하는지는 중요하지 않지만 JSON을 클라이언트 - 서버 통신에 사용하는 것에 대한 이해를 높이려고합니다. 내가 Json.stringify JSON 데이터에 전화를 시도했지만, 그저 내 서버 로그에 GET /download?{%22uuid%22:%224de4c3dd-24db-49dc-8f9e-4d3e0d90dea5%22} 결과.

어떻게 작성하나요?

답변

2

JQuery는 $.ajax()을 사용할 때 기본적으로 GET 메서드를 사용합니다. 그래서 귀하의 콘텐츠가 URL에 추가됩니다.

$.ajax 전화에 type: "POST"을 추가해야합니다. 이렇게하면 jQuery가 URL이 아닌 요청 본문에 데이터를 전송합니다.

또한 응용 프로그램의 routes 구성 파일에 경로가 POST 메서드로 선언되어 있는지 확인하십시오.

+0

예, 이것이 제 문제였습니다. 감사 –

관련 문제