2

I am trying to pass a variable to an jquery ajax request:

$(document).ready(function() {
            // get page number
            var page = parseInt(getUrlParameter('page'));
            if (page < 1) page = 1;
            var page_next = page + 1;

          $.ajax({
                type: "GET",
                data: { 
                    page: page,

                },  
                dataType: 'json', 
           ...

unfortunatelly the variable "page" seems not to be present inside the ajax request. Doing an cosole.log(page); inside reveils "NaN" as a value.

The problem seems to be

if (page < 1) page = 1;

If this value retrieved via get is not present, then page will be NoN.

How can I make sure page is always defined as minimum 1 ?

merlin
  • 2,717
  • 3
  • 29
  • 59
  • 3
    the approach is absolutely correct, the error is probably in the function `getUrlParameter('page')`, which is NOT returning a valid response (or not a number or anything that can be converted to such). Can you please show us that function? – briosheje Jun 09 '15 at 16:22
  • 1
    The only problem with the approach, from the code you've shared so far, is that [you don't have a radix on parseInt](http://stackoverflow.com/questions/6611824/why-do-we-need-to-use-radix), but that wouldn't cause this issue. Whatever `getUrlParameter` is returning is, as briosheje said, not something that can be converted to a number, hence: NaN: Not a Number – Quentin Jun 09 '15 at 16:24
  • the problem seems to be with getURLParameter function as the guys above have pointed out. try with var page = 1; and see if variable contains value 1. – man_luck Jun 09 '15 at 16:29
  • Just narrowed the error down. The problem is within: if (page < 1) page = 1; This does not seem to work if there is no get parameter "page". Will edit question now. – merlin Jun 09 '15 at 16:41
  • @merlin please check my answer, I've suggested a validation in order to default the page to 1 if you get a Nan. – taxicala Jun 09 '15 at 16:50

2 Answers2

1

Your getUrlParameter is returning something that is not convertible to a number NaN = Not a Number, if you share that function maybe we can debug a little bit further the root cause of your issue, in the meantime, I suggest the following validation:

var page = parseInt(getUrlParameter('page'));
if (page < 1 || isNaN(page)) page = 1;
var page_next = page + 1;

That way if your page is not a number, you default it to page 1. Hope this clears things out and helps you.

taxicala
  • 21,408
  • 7
  • 37
  • 66
  • That's fine, but I would rather say that he should fix the getUrlParameter instead of its result. I mean, the result should already be a number, so he should assert that in the **function** rather than after it, especially from the moment that we have no idea of how / where he is taking such data from, right? – briosheje Jun 09 '15 at 16:31
  • Thats why i've edited my question. If He does not share the function there is no way to find the root cause, so I've only made a suggestion in order to make the code more robust. If a user types anything in the url other than a number, it would be nice that the application loads page 1 instead of crashing or displaying an error. – taxicala Jun 09 '15 at 16:33
  • This leads in the right direction. Works if I leave parseInt inside. If I remove it, the page+1 reveals 11 instead of 2. – merlin Jun 09 '15 at 17:03
  • You should leave parseInt in order to make it more robust. And the condition i've wrote is a safe check to default the page to 1 if the user types anything other than a number in the url. – taxicala Jun 09 '15 at 17:05
1

If your problem is within the if statement, the fastest solution I can think about (despite I don't approve it) is this one:

Replace :

var page = parseInt(getUrlParameter('page'));

with:

var page = +getUrlParameter("page") || 1;

if page is falsey (NaN is falsey) it will set it to 1, else to its result.

Example with fiddle:

function getUrlParameter ( param ) {
    return param === "page" ? 5 : false;
}

var page = +getUrlParameter("hello") || 1;
alert(page);

page = +getUrlParameter("page") || 1;
alert(page);

http://jsfiddle.net/51tvdsve/

In any case, you should rather try to fix your getUrlParameter function instead of trying to avoid its output, but that's just my opinion.

briosheje
  • 7,356
  • 2
  • 32
  • 54