سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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. :)

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top