doutiaoku4495 2014-05-19 20:38
浏览 31

jQuery $ .get有时可以工作,有时候不行吗? [关闭]

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?

  • 写回答

1条回答 默认 最新

  • dongyang4615 2014-05-19 20:55
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害