서버 측으로 보내는 데이터를 관리하는 기능을 만들었습니다. 성공했을 때의 응답 을 돌려 주고 싶습니다 $http
.
내 컨트롤러 : (함수를 호출 할 때)
var myResponse = sendData(inputValueArray, url, onSuc, onErr, $http);
// Nothing happen here:
alert(myResponse);
내 데이터 전송 기능 :
function sendData(inputArray, url, onSuccess, onErr, $http) {
var serverResponse = "";
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http({
method: 'POST',
url: url,
data: {
myData: inputArray
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(response) {
if (response.res == true) {
onSuccess();
serverResponse = response;
}
else {
onErr();
}
})
.error(function(data, status, headers, config) {
onErr();
});
return serverResponse;
}
serverResponse
콜백에서 데이터가 있기 전에 반환 됩니다. 대신 다음과 같이 시도하십시오.
function sendData(inputArray, url, onSuccess, onErr, $http) {
var serverResponse = "";
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
return $http({
method: 'POST',
url: url,
data: {
myData: inputArray
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
}
그런 다음 이렇게 사용합니다
sendData
.success(function(response) {
if (response.res == true) {
onSuccess();
serverResponse = response;
alert(serverResponse); // ************
}
else {
onErr();
}
})
.error(function(data, status, headers, config) {
onErr();
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다