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.

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

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM