简体   繁体   中英

calling a REST service from jquery

I have created a REST service at "/post/search/{id}" and when I call this URL from my jquery code sometimes it gets called sometimes not. What is the exact problem in it. Is it regarding jquery or my code. My jquery code is as follows:

    function functionname(clicked_id) {
     $('#idForm').submit(
            function(e) {
        $.ajax({
        type : 'POST',
        url : "${pageContext.request.contextPath}/post/search/"+ clicked_id,success : function(data) {

                }
            });
        });
}

My button code is :

<input type="submit" value="Express Intrest" id="abc" onclick=functionname(this.id) />

try like this.

function yourFunc() {


    $.ajax({
        type : 'POST',
        url : 'yourcontroller/action',
        contentType : "application/json; charset=utf-8",
        dataType : 'json',
        data : param,
        async : false,
        cache: false,
        success : function(dataList) {
            //alert("dataList ---> "+dataList); 



        },

        error : function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(XMLHttpRequest + " - " + errorThrown);
        }
    });


}

pass parameter values in param like this :-

var param;      
    param={param1:"val",param2:"val", param3:"val"};

change your button type from submit to button.

Because when you click on button your page start submitting.so the ajax function is sometime completed not completed before page.

   <input type="button" value="Express Intrest" id="abc" onclick=functionname(this.id) />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM