views:

66

answers:

2

Hi all,

I am writing a web app with asp.net, c# and jquery. Most of the time I'm writing dynamic html to the browser and using different web services to get the content needed.

My call to the service:

function WriteProducts(currentIndex, selectedCategoryId, callback) {
    var d = new Date();
    MyAppServices.GetProducts(selectedCategoryId, currentIndex, 8,
        d.getTime().toString(), callback, function func() {
             alert('failure'); 
        });
}

The request usually gets translated to this (using firebug I monitored it):

http://localhost:8080/MyApp/MyAppServices.svc/GetProducts?categoryId=0&fromIndex=0&toIndex=8&randomNumber=%221271800014441%22

The problem starts when part of the html controls dynamically rendered need to respond to click events. This is when I start using jquery's live method:

$('.filter').live('click', function(event) {
    WriteProducts(0, selectedCategoryId, PopulateDivs);
});

Now from some reason, the request passed to the server becomes this:

http://localhost:8080/MyApp/MyAppServices.svc/GetProducts?categoryId=**%2217%22**&fromIndex=0&toIndex=8&randomNumber=%221271799783355%22

where did these %22 come from? If I take them out, the request passes successfully. I have no idea who inserted these %22, but they are causing havoc here!

Guys, do you perhaps have a clue?

+1  A: 

%22 is the same as ", so I'm guessing that the variable selectedCategoryId is actually a string with the value '"17"'.

Try setting a breakpoint and inspect the value.

Its hard to say something conclusive as you have not included all the relevant code.

Sean Kinsey
+1  A: 

It might be that the date string needs to be converted into a number. It appears that the script is adding quotes around strings. So try this:

function WriteProducts(currentIndex, selectedCategoryId, callback) {
    var d = new Date().getTime();
    MyAppServices.GetProducts(selectedCategoryId, currentIndex, 8,
        d, callback, function func() {
             alert('failure'); 
        });
}

Edit: Updated the answer

fudgey
LOL, thanks for accepting the answer, but I just realized that the time is already a number... so there is no need to convert it to a string then back. So, I've updated the answer :P
fudgey