www.webdeveloper.com
Results 1 to 5 of 5

Thread: any help on explaining what is going on in this function

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    7

    any help on explaining what is going on in this function

    Code:
     function process(){
            if(isValid()){
                var vParameters = new Object();
                vParameters["pvDispoCode"] = $("#pvDispoCode").val();
                vParameters["pvDispoDesc"] = $("#pvDispoDesc").val();
                vParameters["pvActive"] = $("#pvActive").val();
                vParameters["pvTimeStamp"] = new Date().getTime();
    
                $.ajax({
                    url: "mkt_disp_setup_pkg.save_dispo_setup",
                    data:vParameters,
                    success: function(retVal){
                        retJSON = eval("(" + retVal + ")");
                        alert(retJSON.message)
                        if(retJSON.status == "success"){
                            window.location = window.location
                        }
                    },
                    error: function(retVal){
                        alert("Error submitting request. Status: " + retVal.status + " " + retVal.statusText)
                    },
                    async:false
                });
            }
        }
    im new to learning javascript , first post here, and i want to know what is going on , why use ajax here? and if is success does it return the vparameters(data)thanks guys for help

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    Code:
    function process(){
            if(isValid()){
    /* if something is valid */
                var vParameters = new Object();
    /* creating an object which is going to be passed into ajax request */
                vParameters["pvDispoCode"] = $("#pvDispoCode").val();
                vParameters["pvDispoDesc"] = $("#pvDispoDesc").val();
                vParameters["pvActive"] = $("#pvActive").val();
                vParameters["pvTimeStamp"] = new Date().getTime();
    /* now goes the ajax request */
                $.ajax({
    /* target url (some server-side script which supposed to do something with those parameters) */
                    url: "mkt_disp_setup_pkg.save_dispo_setup",
    /* passing parameters */
                    data:vParameters,
    /* callback function which is executing on success */
                    success: function(retVal){
                        retJSON = eval("(" + retVal + ")");
                        alert(retJSON.message)
                        if(retJSON.status == "success"){
                            window.location = window.location
                        }
                    },
    /* actions taken if something went wrong */
                    error: function(retVal){
                        alert("Error submitting request. Status: " + retVal.status + " " + retVal.statusText)
                    },
    /* 
    async Boolean 
    Default: true
    By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the complete/success/error callbacks.
    
     */
                    async:false
                });
            }
        }

  3. #3
    Join Date
    Nov 2013
    Posts
    7
    does function(retVal){ return all the parameters in vparameters ?

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    as i understand, retVal stands for the data which is being recieved from the server-side script. i prefer using jQuery $.get or $.post for their simplier syntax

  5. #5
    Join Date
    Nov 2013
    Posts
    7
    i would think something alike is going on here too
    function addExec(){
    if(isInsertValid()){
    vUrl = "cat_exec_setup_pkg.add_exec_json"
    /* Passing parameters into data parameter on the $.ajax call
    vUrl += "pvFirstName=" + encodeURIComponent($("#pvFirstName").val())
    vUrl += "&pvLastName=" + encodeURIComponent($("#pvLastName").val())
    vUrl += "&pvEmail=" + encodeURIComponent($("#pvEmail").val())
    vUrl += "&pvContactStatus=" + encodeURIComponent($("#pvContactStatus").val())
    vUrl += "&pvActive=" + encodeURIComponent($("#pvActive").val())
    vUrl += "&pvTimeStamp="+new Date().getTime()
    */
    $.ajax({
    url: vUrl,
    data: {pvFirstName: $(''#pvFirstName'').val(),
    pvLastName: $(''#pvLastName'').val(),
    pvEmail: $(''#pvEmail'').val(),
    pvContactStatus: $(''#pvContactStatus'').val(),
    pvActive: $(''#pvActive'').val(),
    pvTimeStamp: new Date().getTime()},
    async: false,
    success: function(retVal){
    retJSON = eval("(" + retVal + ")");
    if(retJSON.status == "success"){
    alert(retJSON.message)
    window.location = window.location;
    }
    else{
    alert("Error: " + retJSON.message)
    }
    },
    failure: function(){
    alert("Cannot process request.")
    }
    });
    }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles