[WEB] 제이쿼리 Ajax

ajax 사용한 소스 기록. 하나씩 하나씩.....


1. :
이미 사용하고 있던 이 소스는 method 라는걸로 get/post 지정 함. 근데 기능이
제대로 안 되길래. 찾아봤더니 method라는게 없네... 뭔지. 암튼 그래서 삭제하고 type으로.
(알고보니 제이쿼리 버전 차이인거 같음.....
method (default: 'GET')
Type: String
The HTTP method to use for the request (e.g. "POST""GET""PUT"). (version added: 1.9.0)
type (default: 'GET')
Type: String
An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0.
)
그리고 dataType을 json으로 했더니 계속 에러남.  참고로 data4가 배열임.
암튼 dataType 지우니 잘됨. 원인 파악은 안 했음. 아직.

가)


                var params = {
                            'aaa' : 'data1',
                            'bbb' : data2,
                            'ccc' : data3,
                            'ddd' : data4
                    };

                var request = $.ajax({
                        type: 'POST',
                        url: 'aaa_bbb.php',
                        data: params,
                    });
             //응답
                request.done(function( datas ) {  //리턴값 받음.
                    console.log(datas);
                    alert("수정 완료되었습니다.("+datas+")");
                    window.location.reload();
                });
            //실패
                request.fail(function( jqXHR, textStatus ) {
                    console.log( "Request failed: ", textStatus );
                });

  • jqXHR.done(function( data, textStatus, jqXHR ) {});
    An alternative construct to the success callback option, refer to deferred.done() for implementation details.
  • jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
    An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.
나)

$re_aaa = $_POST["aaa"];
$re_bbb = $_POST["bbb"];
$re_ccc = $_POST["ccc"];
$re_ddd = $_POST["ddd"];


echo json_encode($re_aaa);   //리턴값

2.

function 함수명() {
var sStep = $("#셀렉트").val();
var oPost = {
"step" : sStep,
                         "type" : "book"
};
var sUrl = "./ajax/주소.php?" + oPost
$.ajax({
"url"      : sUrl,
"type"     : "GET",
"dataType" : "json",
"success"  : function(jData) {
if(jData.result == "success") {
$("#원하는곳").val(jData.리턴된값);
}
else {
if(jData.msg)
alert(jData.msg);
else
alert("오류가 발생하였습니다.");
}

},
"error"    : function(oXhr) {
alert("오류가 발생했습니다");
console.dir(oXhr);
}
});

}










댓글

이 블로그의 인기 게시물

[문서] excel 체크박스 삭제

[DB] MySQL 백업 / 복원

[WEB] ASP.NET System.NullReferenceException: 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.