반응형
$.199에서 $160까지
이 jQuery 코드는 발신기지 간에 정상적으로 동작합니다.
jQuery.ajax({
url: "http://example.appspot.com/rest/app",
type: "POST",
data: JSON.stringify({"foo":"bar"}),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response) {
console.log("success");
},
error: function (response) {
console.log("failed");
}
});
이제 이 코드를 Angular.js 코드로 변환하려고 합니다.
$http({
url: "http://example.appspot.com/rest/app",
dataType: "json",
method: "POST",
data: JSON.stringify({"foo":"bar"}),
headers: {
"Content-Type": "application/json; charset=utf-8"
}
}).success(function(response){
$scope.response = response;
}).error(function(error){
$scope.error = error;
});
아무쪼록 잘 부탁드립니다.
앵귤러$http를 호출하는 JS 방식은 다음과 같습니다.
$http({
url: "http://example.appspot.com/rest/app",
method: "POST",
data: {"foo":"bar"}
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
$scope.data = response.data;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
$scope.error = response.statusText;
});
또는 숏컷 방법을 사용하여 보다 간단하게 작성할 수 있습니다.
$http.post("http://example.appspot.com/rest/app", {"foo":"bar"})
.then(successCallback, errorCallback);
주의할 점은 다음과 같습니다.
- AngularJS 버전이 더 간결합니다(특히 .post() 메서드를 사용).
- AngularJS는 JS 객체를 JSON 문자열로 변환하고 헤더를 설정합니다(커스터마이즈 가능).
- 콜백 함수는
success
그리고.error
각각 (각 콜백의 파라미터도 메모해 주세요) - 각도 v1.5에서는 권장되지 않습니다. - 사용하다
then
기능을 합니다. - 상세 정보
then
사용법은 이쪽에서 확인하실 수 있습니다.
위의 내용은 간단한 예시와 몇 가지 포인트에 불과합니다. 반드시 Angular를 확인하십시오.상세한 것에 대하여는, JS의 메뉴얼을 참조해 주세요.
AngularJs에서 http 서비스를 사용하여 Ajax 요청을 구현하여 원격 서버에서 데이터를 읽거나 로드할 수 있습니다.
$120 서비스 방법은 다음과 같습니다.
$http.get()
$http.post()
$http.delete()
$http.head()
$http.jsonp()
$http.patch()
$http.put()
다음 중 하나의 예:
$http.get("sample.php")
.success(function(response) {
$scope.getting = response.data; // response.data is an array
}).error(){
// Error callback will trigger
});
http://www.drtuts.com/ajax-requests-angularjs/
다음을 사용할 수 있습니다.
angular-post-fix 다운로드: "^0.1.0"
그런 다음 각도 모듈을 선언하면서 의존관계에 'httpPostFix'를 추가합니다.
참고 자료 : https://github.com/PabloDeGrote/angular-httppostfix
$.disc를 사용하여 데이터를 할당할 수 있습니다.
$http({
url: "http://example.appspot.com/rest/app",
method: "POST",
data: $.param({"foo":"bar"})
}).success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
이것 좀 봐: 각진JS + ASPNET Web API 도메인 간 문제
언급URL : https://stackoverflow.com/questions/12131659/from-jquery-ajax-to-angular-http
반응형
'programing' 카테고리의 다른 글
HTML jQuery의 데이터 속성에 JSON 개체 저장 (0) | 2023.02.16 |
---|---|
JSON 개체 내 Javascript 검색 (0) | 2023.02.16 |
루비 해시 개체를 JSON으로 변환하려면 어떻게 해야 합니까? (0) | 2023.02.16 |
react-router-dom v4의 여러 네스트 루트 (0) | 2023.02.16 |
self.tableView.reloadData()가 Swift에서 작동하지 않음 (0) | 2023.02.16 |