weixin_33724046 2018-06-05 00:02 采纳率: 0%
浏览 36

无法调试Ajax

I'm having a lot of trouble debugging an ajax request. The same call works well on an older version of the site on the same server. It's a piece of code which sends data from Wordpress to Infusionsoft. I've checked and that's all connecting ok. I've also installed a CORS headers plugin thinking that might be the issue, but apparently not. With the following I get a parseerror:

  var r=confirm("Are you sure you wish to create the '" + profile.title + "' email template?");
    if (r==true) {
        $('#mailout_buttons').hide();
        $('#mailout_progress').show();
        var request = $.ajax({
            type : "post",
            dataType : "jsonp",
            url : ajaxurl,
            data : $.extend(profile, {action: "create_weekly_email"}),
            xhrFields: {
                withCredentials: true
            },
            error: function(XHR, textStatus, errorThrown)
            {       
                alert(textStatus);                                 
            },
            success: function(response) {
                if(response.type == "success") {
                    $('#mailout_buttons').show();
                    $('#mailout_progress').hide();
                    $('#mailout_success').show();
                }
                else {
                    alert("Error with template creation")
                }
            }
        });
        request.done(function(msg){ console.log(msg)});
        request.fail(function(jqXHR, textStatus){ console.log("Request failed: " + textStatus)});

and if I change out the error to:

            error: function(e)
            {       
                alert(JSON.stringify(e), null, 4);                                 
            },

I get:

{"readyState":4,"responseText":"create{\"type\":\"success\"}","status":200,"statusText":"OK"}

I've also tried posting with text and html. Using html, it tells me the JSON is not well formed, although using the https://jsonformatter.curiousconcept.com/, it says it is. Using text, it falls through to the else in the success callback.

EDIT 1: I've posted the create_weekly_email function below. The create_weekly_email calls get_weekly_email which has an sql query in it. I've hard coded a couple of vars into it, which return results in both phpmyadmin and in the responseText var. I can see that there is some malformed html returned ( which creates an office document ), and the length of the html varies. I don't want to post returned data here, but in this last call it's missing all the closing tags. Sometimes the returned string is much shorter, which makes me think there might be some kind of timeout issue?

function create_weekly_email() {

    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        header("Location: " . $_SERVER["HTTP_REFERER"]);
        exit;
    }


    $result = ["type" => "error"];
    if($profile = filter_input_array(INPUT_POST, massemail_fields())) {
        if($re = create_weekly_mail_template($profile)) {
            $result = ["type" => "success"];
        }
    }
    wp_send_json($result);
    exit;
}
  • 写回答

1条回答 默认 最新

  • weixin_33733810 2018-06-05 10:06
    关注

    Try this script.

    just replace this dataType : "jsonp", with dataType : "json",.

    var r=confirm("Are you sure you wish to create the '" + profile.title + "' email template?");
    if (r==true) {
        $('#mailout_buttons').hide();
        $('#mailout_progress').show();
        var request = $.ajax({
            type : "post",
            dataType : "json",
            url : ajaxurl,
            data : $.extend(profile, {action: "create_weekly_email"}),
            xhrFields: {
                withCredentials: true
            },
            error: function(XHR, textStatus, errorThrown)
            {       
                alert(textStatus);                                 
            },
            success: function(response) {
                if(response.type == "success") {
                    $('#mailout_buttons').show();
                    $('#mailout_progress').hide();
                    $('#mailout_success').show();
                }
                else {
                    alert("Error with template creation")
                }
            }
        });
        request.done(function(msg){ console.log(msg)});
        request.fail(function(jqXHR, textStatus){ console.log("Request failed: " + textStatus)});
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行