tags:

views:

73

answers:

2

I'm trying to build a reusable $.ajax method for my current application but I'm struggling to find solid information about how to build the data: section dynamically ...

Currently I'm looking at something similar to the function below, but I'm not sure how to implement this w/ jQuery 1.3.x (or if this type of thing was moved into the core?)

var extraParams = {
        timestamp: +new Date()
    };

    $.each(options.extraParams, function(key, param)
    {
        extraParams[key] = typeof param == "function" ? param() : param;
    });
+1  A: 

I use this method as a wrapper so that I can send parameters. Also using the variables in the top of the method allows it to be minimized at a higher ratio and allows for some code reuse if making multiple similar calls.

function InfoByDate(sDate, eDate){
var divToBeWorkedOn = '#AjaxPlaceHolder';
var webMethod = 'http://MyWebService/Web.asmx/GetInfoByDates'
var parameters = "{'sDate':'" + sDate + "','eDate':'" + eDate + "'}"

  $.ajax({
                type: "POST",
                url: webMethod,
                data: parameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {    
                        $(divToBeWorkedOn).html(msg.d);
                },
                error: function(e){
                        $(divToBeWorkedOn).html("Unavailable");                          
                }
        });
Bobby Borszich
+1  A: 

This part of your question:

typeof param == "function" ? param() : param;

Should be written in current jQuery syntax as

$.isFunction(param) ? param() : param;

I hope that helps you build what you want.

dyve