douyi1944 2018-04-26 20:40
浏览 7


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:


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:

  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:

        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.

        'sowp_js', // this is the script name that should be used to enqueue the JavaScript code above
            '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 I350 Gigabit Network
    • ¥15 关于#abap#的问题,请各位专家解答!
    • ¥20 内网通过公网访问外网问题
    • ¥20 谁有这个东西 继续教育的
    • ¥15 怎么使请求通过cors
    • ¥15 WDM 驱动ACPI 相关疑问
    • ¥15 prism 跨窗体共享数据绑定 wpf
    • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
    • ¥15 0基础计算机毕设,应该从哪开始?
    • ¥60 使用DKT40脑图划分ROI区域