0

I'm sending a date to my webservice in this format: 12/14/2010 but the "/" character is causing the posted value to be something like 0.0323483238432834. How do I literally send the date in that format? Heres the ajax code I use:

function createNewPromo() {
            var a = $("#txtDateStart").val();
            var b = $("#txtDateEnd").val();
            $.ajax({
                type: "POST",
                url: "/WebService_VehicleDisplay.asmx/createNewPromo",
                data: "{startDate:" + a + "," + "endDate:" + b + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    var response = msg.d;

                },
                failure: function (msg) {
                    alert('failure');
                }
            });
        } 

Edit: here is what firebug says. endDate/startDate are my parameters.(this is what is being posted to my web service.).

    JSON

endDate
    0.00021321961620469082

startDate
    0.00028429282160625445
Source
{startDate:12/21/2010,endDate:12/28/2010}
5
  • 1
    Are you sure it is posted that way rather than interpreted like that on the webservice? Can you post your webservice code? Can you show a dump of the request? Commented Dec 12, 2010 at 22:14
  • I am using firebug, it shows the source(dates in textboxes) but then shows that json sends the date as a bunch of numbers. Commented Dec 12, 2010 at 22:27
  • Can you post the full text of the request from firebug? Commented Dec 12, 2010 at 22:35
  • Better yet, is your site live? Commented Dec 12, 2010 at 22:36
  • @Oded, it has nothing to do with my webservice, the date is being taken from my textbox correctly,but then for some reason it is treating the date like an integer and giving me the answer for 12 divided by 21 and that answer divided by 2010... Commented Dec 13, 2010 at 23:28

3 Answers 3

3

I figured it out, there needs to be a single quote on each side of the parameter value like so:

data: "{startDate:'"+ a +"',endDate:'"+ b +"'}"

I included a great article in my comment below about this issue. Here is a snippet from it:

"Again, remember that a Content-Type of application/json is a requirement when working with ASMX ScriptServices. By setting that Content-Type on the request, you’ve committed to sending JSON serialized parameters, and a URL encoded string is far from valid JSON."

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm...interesting. I don't see any mention of a need for ' on the jQuery docs.
@metthewpavkov, there is a few stipulations about this. You don't need it for regular strings, but if there is arithmatic operators in the string it will try to evaluate it. The following URL goes into depth about this issue and actually brings up how the jQuery documentation is subtle about the syntax: encosia.com/2010/05/31/…
0

Use encodeURIComponent() on the string.

2 Comments

If you read the link you gave me it says "This function encodes special characters, with the exception of: * @ - _ + . /".
@nick Oops. You could try encodeURIComponent(). Also, if you do an alert(a + b) before your ajax, do you get the correct dates?
0

The jQuery docs says:

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".

So, you have to manually serialize the data as JSON if you want to send it as JSON (using json.org or similar)

var data = {param1: 'value1', param2: 'value2', paramArray: ['a','b','c']}; 
$.ajax({ 
    ...
    data: JSON.stringify(data), // using json.org
    ... 
});

2 Comments

i'm confused, this is how i'm handling the paramters. Except your syntax is wrong, their has to be quotes after data like how I have it.
@nick sorry, I misunderstood your question, fixed my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.