Vicki
Vicki

Reputation: 1456

Serialize a date with AJAX

How do I serialize a date to use for ajax? I am using ajax to send a date over from a hidden input textbox and want to serialize it to run a query with coldfusion.

HTML (automatically receives date)

JS

var Tmrwdate = $('#TomorrowsDate'); 
console.log($(Tmrwdate).serialize());
    $.ajax({
            url: "proxy/TomorrowsDate.cfm",
            type: "post",
            dataType: "json",
            data: {date: Tmrwdate.serialize() },
            success: function (data) {
                console.log(data);
            }, 
            error: function (xhr, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });

proxy/TomorrowsDate.cfm

<cfset session.dealerwork.tomorrowsdate = form.TomorrowsDate >

<cfquery name="tomorrowTextArea">
    SELECT *
    FROM dbo.Dealer_Track_Work
    WHERE Date_Due = <cfqueryparam value="#session.dealerwork.tomorrowsdate#" /> 
    AND Date_Complete IS NULL       
</cfquery>


<cfoutput>#SerializeJSON(session.dealerwork.tomorrowsdate)#</cfoutput>

my console log from console.log($(Tmrwdate).serialize());

TomorrowsDate=10%2F28%2F2015

Upvotes: 1

Views: 1493

Answers (2)

beloitdavisja
beloitdavisja

Reputation: 1509

You shouldn't need to serialize a date, just send the value in your data argument.

JS

var Tmrwdate = $('#TomorrowsDate').val(); 

$.ajax({
        url: "proxy/TomorrowsDate.cfm",
        type: "post",
        dataType: "json",
        data: {date: Tmrwdate },
        success: function (data) {
            console.log(data);
        }, 
        error: function (xhr, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });

proxy/TomorrowsDate.cfm

<cfset session.dealerwork.tomorrowsdate = form.TomorrowsDate >
<cfset result = { TomorrowsDate = form.TomorrowsDate} />

<cfquery name="tomorrowTextArea">
    SELECT *
    FROM dbo.Dealer_Track_Work
    WHERE Date_Due = <cfqueryparam value="#session.dealerwork.tomorrowsdate#" /> 
    AND Date_Complete IS NULL       
</cfquery>


<cfoutput>#SerializeJSON(result)#</cfoutput>

Upvotes: 3

jstuartmilne
jstuartmilne

Reputation: 4508

You can format the date however you want or really your endpoint is expecting it

function formatMyDate(date){

var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day+'/'+monthIndex+'/'+year;
}



$.ajax({
        url: "proxy/TomorrowsDate.cfm",
        type: "post",
        dataType: "json",
        data: {date: formatDate(new Date()) },
        success: function (data) {
            console.log(data);
        }, 
        error: function (xhr, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });

Or you can avoid re-inventing the wheel and use something like momentjs http://momentjs.com/

$.ajax({
        url: "proxy/TomorrowsDate.cfm",
        type: "post",
        dataType: "json",
        data: {date: moment().format("YYYY-MM-dd") },
        success: function (data) {
            console.log(data);
        }, 
        error: function (xhr, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });

Upvotes: 1

Related Questions