douyi1944 2018-04-26 20:40
浏览 7
已采纳

用ajax错误的请求网址?

So, I'm trying to send a post request to a php script of mine. On the web I'm in a page like: https://yyy.yyy/test-page My ajax url is set as such:

url: "https://yyy.yyy/test.php"

But then it returns a 404 error with the following url:

https://yyy.yyy/test-page/test.php

And it definitely won't find in the php file in that path, since it is located at root. Moving the php to another folder is not an option. This website is located in a WordPress server.

Here's the js code:

$.ajax({
  url: "https://yyy.yyy/test.php",
  method: "POST",
  type: "Json",
  data: data,
  success: function (res) {...}
});
  • 写回答

1条回答 默认 最新

  • dpii73380 2018-04-26 21:23
    关注

    If you're wanting to make use of Ajax and are running WordPress, you should look into writing a plugin and making use of the hooks and functions available to make this easier as WordPress will do a lot of heavy lifting.

    I see you mentioned it can't be moved, but if it's at all possible to at least try and replicate the PHP code into a plugin (or a theme although that's less ideal and might not work properly) then it'll make things easier.

    Take a look at using Ajax in plugins.

    The JavaScript (jQuery) bit:

    $.ajax({
        data: {
            action: 'my_ajax_request'
        },
        type: 'post',
        url: SOWP_AJAX.admin_ajax,
        success: function(response) {
            console.warn( response );
        }
    });
    

    The PHP bit (Part 1):

    This ensures that the URL that you post your Ajax function to is mapped to an actual URL. In this case, the WordPress Ajax handler at admin-ajax.php.

    wp_localize_script(
        'sowp_js', // this is the script name that should be used to enqueue the JavaScript code above
        'SOWP_AJAX',
        array(
            'admin_ajax' => admin_url('admin-ajax.php')
        )
    );
    

    The PHP bit (Part 2):

    Put this inside a plugin or theme file that is activated. The hook my_ajax_request must match the request action in the Ajax request and the function name so it's called correctly.

    add_action( 'wp_ajax_my_ajax_request', 'my_ajax_request' ); // the hook that is triggered when the request is made
    
    function my_ajax_request() {
    
        echo 'my ajax request works'; // print something out
    
        wp_die(); // stop script execution
    
    }
    

    Once you've got all of the above in place, when the Ajax request is working and runs, you should see my ajax request works printed in your browsers console. If it returns 0 then it means the request failed somewhere. If it does fail, it usually means the action hook has been registered and called so something might be spelt wrong or missing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联