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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘