2016-06-10 2 views
0

Google Apps Script를 사용하여 POST 요청의 매개 변수에 현재 액세스하려고합니다. e.parameter.name을 사용하여 객체의 키에 액세스 할 수는 없지만 e.parameter를 사용하여 params 객체를 로거 할 수 있습니다.XMLHttpRequest Google Apps Script의 매개 변수에 액세스

XMLHttpRequest를

var http = new XMLHttpRequest(); 
    var url = "myappURL"; 
    var params = JSON.stringify({employeeStatus: "Active", name: "Henry"}); 

    http.open("POST", url, true); 

    //Send the proper header information along with the request 
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    //Call a function when the state changes. 
    http.onreadystatechange = function() { 
    // call back function 
    } // end callback 

    http.send(params); 

Google 애플리케이션 스크립트

function doPost(e) { 
    if (typeof e !== 'undefined') { 
    Logger.log(e.parameter.name); // does not work (undefined) 
    } // end if 
} // end doPost 
+0

아마도이 스크립트의 공개 버전을 볼 수 있을까요? 전자 변수의 내용을 기록 할 때 – Sudsy

+0

는 @Sudsy [링크] (https://script.google.com/d/1MxBNwUoG4I_F0Gt1GGYzNrr3m6PakhcDFxIA_g-nvoS-bRHx0IS5jgMJ/edit?usp=sharing) –

답변

0

데이터가 HTTP를 통해 제기되고있는 여러 가지 방법에 미묘한 단점이있다. 예를 들어 json 데이터의 일반적인 헤더가 Content-Type : application/json 일 때 Content-type "application/x-www-form-urlencoded"를 사용하고 있음을 알 수 있습니다.

반환되는 내용을 볼 수 있도록 e 변수의 내용을 반환하는 줄을 추가했습니다.

curl을 사용하여 다음 명령으로 디버깅했습니다. 내가받은

curl -H "Content-Type: application/json" ---data "{status:123}" https://script.google.com/macros/s/AKfycbyJ38V-HpG7A-DxIBpik4HJ89fAtnCemCJ7ZXeFEL8KPEuGsR8/exec 

응답했다 :

{"parameter":{},"contextPath":"","contentLength":12,"queryString":null,"parameters":{},"postData":{"length":12,"type":"application/json","contents":"{status:123}","name":"postData"}} 

당신이 내 경우에는 JSON이 내용 필드가 아닌 매개 변수에 반환 볼 수 있습니다.

당신은 당신이 얻는 것을보기 위해 당신의 스크립트로 이것을 시도 할 수 있습니다. Content-Type을 변경해보십시오.

추가 테스트를 마친 후에는 json보다는 양식 데이터를 제출하는 것이 좋습니다. 귀하의 자바 스크립트를 수정하여 다시 패러레이터를 얻을 수있게되었습니다 :

var http = new XMLHttpRequest(); 
var url = "https://script.google.com/macros/s/AKfycbyJ38V-HpG7A-DxIBpik4HJ89fAtnCemCJ7ZXeFEL8KPEuGsR8/exec"; 
var params = "employeeStatus='Active'&name='Henry'"; 

http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

//Call a function when the state changes. 
http.onreadystatechange = function() { 
    if (http.readyState==4) { 
    //alert the user that a response now exists in the responseTest property. 
    console.log(http.responseText); 
    // And to view in firebug 
    // console.log('xhr',xmlhttp) 
    } 
} // end callback 

http.send(params); 
+0

,이 결과 '= {{{파라미터이고 "employeeStatus": "활성", "이름": "Henry"} =}, contextPath =, contentLength = 42, queryString = null, 매개 변수 = {{ "employeeStatus": "활성", "이름": "Henry"} = [Ljava.lang.Object; @ 68bdf26b}, postData = FileUpload} ' –

+0

편집 된 답변 – Sudsy

+0

에서 전체 해결책을보십시오. 이제 작동 중입니다. 당신의 대답은 훌륭합니다! –

관련 문제