douqi0090 2012-09-08 09:02
浏览 29

jQuery AJAX没有发送到我的PHP程序

I'm no expert in AJAX (or jQuery) but I thought what I was doing was pretty easy yet when I send an ajax request with:

$.ajax ( requestObj );

it doesn't send and I'm hoping someone can help. In order to give context, I've set the "requestObj" up as follows:

    //initialise a request object
    var requestObj = {};
    requestObj.response = 'ajax-response';
    requestObj.type = 'POST';
    requestObj.url =  my_config['ajax-service-list'][service]['url'];
    requestObj.data = $.extend ( requestObj.data , {
                                            action: service,
                                            other: parameters,
                                            _ajax_nonce: my_config['ajax-service-list'][service]['nonce']
    });
    requestObj.global = false;
    requestObj.timeout = 30000;
    requestObj.success = function ( r ) {
        alert ( "Success: " + r );
    }
    requestObj.error = function ( r ) {
        console.log ("FAILURE WITH AJAX Call ( " + JSON.stringify (r) + ")");
    }

There's one thing that probably needs explaining. The two references to "my_config" are references to a Javascript variable that I set using Wordpress's wp_localize_script() function. Basically it just provides context about where to find the URL, the NONCE to use, etc. I have tested that the URL and NONCE information is working correctly so that shouldn't be the problem. For example, I put a breakpoint on the browsers debugger on the line after the two references are defined and got these results: debugger output of requestObj properties

When I call the ajax function it immediately executes the success function and sends in the value of 0. Looking at my PHP error logs though I can see that the request was never sent. What could be getting in the way of $.ajax(requestOb) from actually sending the request?

UPDATE:

Thanks to Michael's sage advice I realised that I am in fact getting a request to go out but as it's running in a local environment the response is coming back lightening fast. Now I am suspecting this has more to with Wordpress configuration. I have hooked into the wp_ajax_[service_name] but it immediately returns 0. I'll re-ask this question with this new information in the wordpress forum.

  • 写回答

3条回答 默认 最新

  • duannao3402 2012-09-08 09:27
    关注

    At first look, it looks like your URL has spaces around get_action_template. That might be an issue.

    Also, passing dataType might help.

    If not try getting a JSON response without any parameters and post the output

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用