0

FINAL UPDATE: FIXED!

I have literally no idea what this had to do with the problem whatsoever but apparently due to coding of my main file it changed <form action='something.php'> to <form action='something.php#12'>. "#12" then appeared at the end of my URL and somehow successfully caused the networking settings to act weirdly. After filtering this character from the URL, everything seems to be alright.

Any idea how in the literal hell could this break jQuery's $.get? o.O

Anyway, it works now. Thanks so much for help guys! :)


I know I'm noobish as hell but anyway.

I have a PHP code, and the thing I'm having problem with is launching a script (in this case liking a status) without reloading the page. I have solved that with a simple onClick() that after clicking executes a jQuery $.get command.

My problem is that sometimes the script executes, sometimes not! I've got no idea why! I have tried all possible methods of debugging yet nothing helps. This is really weird!

echo "<a onclick=\"$.get('like.php?trackid=$rowtracks[trackid]');    return false\">Give a ♥</a>";

Please excuse me for any stupid mistakes I may have made, I mostly need to fix the main problem.

Even when I tried to copy the Javascript code to the Chrome console, it sometimes works sometimes not ._. What the hell?

UPDATE: In the network tab of the Chrome debug system, I can see "Method: GET | Status: Pending" "1 requests | 0B transferred" instead of "Method: GET | Status: 200 OK" "1 requests | 540B transferred" ... so sometimes it gets reply whilst sometimes it doesn't. Any help?

user3453428
  • 166
  • 2
  • 10
  • 2
    the use of **onclick** is an unusual jQuery approach –  May 19 '14 at 20:40
  • 4
    What does sometimes mean here? In some browsers? Some times after loading the page? Some time if you click multiple times in a row? – Mattias Åslund May 19 '14 at 20:43
  • suggest you read this: http://stackoverflow.com/questions/12627443/jquery-click-vs-onclick –  May 19 '14 at 20:44
  • 1
    Do you see any error messages in the browser console? Like $rowtracks not being defined or trackId pointing ouside the array? – Mattias Åslund May 19 '14 at 20:46
  • How do you know it sometimes doesnt work? Have you watched the network tab in the developer tools and not see a request being fired? – Danny May 19 '14 at 20:46
  • 1
    As far as I saw, absolutely randomly. Last time I tried it today at the morning (not a single line of code changed since then) and it was okay. Now I tried it _again_ and it doesn't. No it doesn't give any errors in the console, and to the browsers - I have just realized that **right now**, it **doesn't work in Chrome but it does in IE**. Woah. – user3453428 May 19 '14 at 20:52
  • 2
    When it doesn't work, what does the network tab tell you? it's likely that this problem has nothing to do with javascript and any solution people post here regarding changing the way you bind the events won't help. – Kevin B May 19 '14 at 21:06
  • "Method: GET | Status: Pending" "1 requests | 0B transferred" – user3453428 May 19 '14 at 21:16
  • And, if you wait until it is no longer pending, what happens? – Kevin B May 19 '14 at 22:10

1 Answers1

2

In jQuery you don't use the on... attributes to handle events of tags - instead you should bind event's to a selector.

<a data-src="/like.php?trackid=$rowtracks[trackid]" class="ajax-link">Give a ♥</a>

and the following js

$('.ajax-link').on('click', function(e) {
    $.get($(this).data('src').split('#')[0]);
    e.preventDefault();
});

to prevent some 404's which could occur, if you use this script in different paths, I added the / to the uri

Philipp
  • 15,377
  • 4
  • 35
  • 52
  • 1
    While this is a good suggestion, it doesn't change anything about how the request is being made, which means the error will likely still occur. – Kevin B May 19 '14 at 21:08
  • Yes it does occur. :( Thanks for the tips though. I am not very good at JS :/ – user3453428 May 19 '14 at 21:11
  • 2
    if the error still occurs, take a look at your browsers developer tools(F12) under the network section - maybe you get some usefull information about your request in the response section of the related request – Philipp May 19 '14 at 21:25
  • Yes I just did! I updated the question. if anyone can help me with why the script doesn't respond, it'd help a lot. – user3453428 May 19 '14 at 21:28
  • are you sure, that you haven't any extensions installed, which could block the request(i.e. adblock?) – Philipp May 19 '14 at 21:30
  • I believe [`.data()`](http://api.jquery.com/data/) is not the correct way to get the value of the data-src attribute. I think you want `attr('data-src')`. – Jakob Jingleheimer May 19 '14 at 21:31
  • Philipp, even after disabling adblock it didn't change anything. :/ – user3453428 May 19 '14 at 21:33
  • "Description: Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute" taken from your link.. – Philipp May 19 '14 at 21:33
  • one more thing - you could try to add an random get param to your request to prevent some caching side effects. I just updated the example code – Philipp May 19 '14 at 21:36
  • Philipp, I have found out a pretty weird thing ... Sometimes a "#12" randomly appears at the end of my URL. When it does, $.get doesn't work ._. So that's the problem! Any way to filter it out? – user3453428 May 19 '14 at 21:51
  • `$.get($(this).data('src').split('#')[0])` – Philipp May 19 '14 at 21:53