1

Works:

    function jsUpvote(photo_id, username) {

        //var getURL = "http://www.uglyfacez.com/gallery/upvote.php?photo_id=" + photo_id + "&username=" + username;

        $.get("http://uglyfacez.com/gallery/upvote.php?photo_id=15&username=user000",
            function(returnValue){
                // do stuff here
        });
    }

I wrote the function above to run a php script on the page and pass in the variables photo_id and username to the script through the URL. When I hard code it, as above, it works just fine, but when I give it the javascript variables (as you can see in getURL), it won't work at all. For example, this is what I want to do, but will not work:

$.get("http://www.uglyfacez.com/gallery/upvote.php?photo_id=" + photo_id + "&username=" + username,
            function(returnValue){
                // do stuff here
        });

Why will this not work and what is the solution?

EDIT: I discovered what the problem was. For some reason, including www in my GET url keeps it from receiving a response from the server. Once removed from the URL, it works just fine.

3
  • I'm sorry you feel that way, but if it worked, I wouldn't be here. Nov 12, 2011 at 2:26
  • @RussellStrauss: Tomalak was trying to say that " it does not work " is a completely useless statement and does not help in solving the problem. It is generally expected that OP gives full information about the error and shows some effort in investigating the issue before posting it. Other people are donating their time to solve the issue, so some minimum effort from OP's side is rather very welcome.
    – Tadeck
    Nov 12, 2011 at 2:33
  • Sorry, what I meant by that is "the block of code is not executing and I can't figure out why." I didn't realize I didn't mention that the first time around. Nov 12, 2011 at 2:48

3 Answers 3

2

This seems to be an issue regarding cross domain requests.

You can use some solutions such as JSONP or you can load script from different domain (in this case: www subdomain). To see different aspects and settings regarding cross-domain, please go to the .ajax() documentation page (and search for " cross-domain " or " crossDomain ").

The issue is basically a problem connected to same origin policy. On the mentioned documentation page you can read, that:

  • Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
  • Script and JSONP requests are not subject to the same origin policy restrictions.
1

Here is the sysntax for the jquery .get function:

jQuery.get( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )

something like:

$.get("http://www.uglyfacez.com/gallery/upvote.php", {'photo_id':photo_id,'username':username},
            function(returnValue){
                // do stuff here
        });

try to follow the syntax first put the data on the parameters of the function, I think this link might help you as well:

http://api.jquery.com/jQuery.get/

Or if you still encounter the same problem try to open the link manually on your browser again and check if it returns any error just to be sure that it returns your expected output. :)

2
  • Opening the link manually in my browser works just fine and doesn't cause any errors. This is how I know it is a problem with my .get call. Nov 12, 2011 at 2:26
  • Do you see any errors on firebug when the code is being triggered, if it runs fine on the actual site your doing the request then it is caused by an error on the javascript code you have, if firebug is returning any javascript bug it would be better if you post it here so we could detect problem easier. :) Nov 12, 2011 at 2:34
0

Are you setting photo_id and username properly?? More code might help. What's the error message? Are you using Firebug or Chrome to see if you are getting an error?? Seems as though photo_id and/or username are not being passed in properly.

2
  • Yes, both photo_id and username are getting passed in and set correctly. If I do alert(getURL); it gives me the exact url that I want. If I pass in 15 for photo_id and user000 for username, it will give me the exact URL written above. There isn't any more code to give that I am using. You can see the error here: i.imgur.com/Rujzb.png The code in the function(returnValue) block simply doesn't run at all. I'm using Firebug to debug. Nov 12, 2011 at 2:12
  • Here's my next question. Are you using any type of tool to see what is being sent or returned from the URL? I'm still thinking that maybe there is a space or something that is being missed. Nov 12, 2011 at 2:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.