简体   繁体   English

如何使用 Ajax 运行 php 脚本(无返回)

[英]How do I just run a php script with Ajax (no returns)

I'm trying to run a php script with ajax.我正在尝试使用 ajax 运行 php 脚本。 All I want for it to do is just run the script, I don't want any echos or anything.我想要做的只是运行脚本,我不想要任何回声或任何东西。 Is there a way to do this.有没有办法做到这一点。 Here is what I've tried:这是我尝试过的:

        $('button')[1].click(function () {
          $.ajax({
            method: 'get',
            url: 'like.php',
            data: {
              id: $('button')[1].id
            },
            success: function(data) {
              console.log(data);
            }
          });

I thought that this would just run like.php with the get data I sent it but it is not working.我认为这会像.php 和我发送的获取数据一样运行,但它不起作用。 I know that the php script works because when I type in the url with the id parameter manually it works.我知道 php 脚本可以工作,因为当我手动输入带有 id 参数的 url 时,它可以工作。

This will clean up and work better for your "fire n forget" like buttons.这将清理并更好地为您的“fire n forget”之类的按钮工作。 Add a special class to only the buttons you want to do this:仅将特殊的 class 添加到您要执行此操作的按钮:

<button class="like-it">I Likes!</button>

Then the jquery handler can be this:那么 jquery 处理程序可以是这样的:

$(document).ready(function() {
    $('body').on('click','.like-it',function(e){

        e.preventDefault(); // stops the button from doing something else
        $.get(  'like.php',
                { id : $(this).attr('id') },
                function(response) { console.log(response); }
             );

    });
});

You can test if your PHP is doing something, by simply returning something and inspecting the result in your console tab of the browser devtools.您可以测试您的 PHP 是否正在做某事,只需返回一些内容并在浏览器开发工具的控制台选项卡中检查结果即可。 Since you are ignoring the result, you can echo anything for debugging in devtools.由于您忽略了结果,因此您可以回显任何内容以在 devtools 中进行调试。 Then comment out the echo when you go to live.然后注释掉你go住时的回声。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM