weixin_33709219 2018-06-16 04:25 采纳率: 0%
浏览 96

为什么Ajax请求未完成?

我正在制作一个表单来订阅时事通讯,为此我使用Ajax请求调用MailChimpAPI。API工作得很好,我已经测试过了。但是在Ajax代码中,我明明在“发送前”和“完成”部分设置了警告,但是什么反应都没有。

这是我的代码:

HTML表单代码

<form id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate">
    <input type="email" value="" name="EMAIL" class="email" id="useremail" placeholder="Type our mail address" required>
    <input type="button" value="subscribe" name="subscribe" id="mc-embedded-subscribe" class="mc-button" >
      <div id="subscribe-result">
      </div>
</form>

脚本代码

<script>
$("#mc-embedded-subscribe").click(function() { 

var email = $('#useremail').val();

$.ajax({
    type: 'post',
    url: "newslatter.php", 
    dataType: 'json',
    data: 'useremail='+email,
    beforeSend: function() {
    },
    complete: function() {
    },  
    success: function(data)
    {
        alert("enter success");
        if(data.type == 'error')
        {
            output = "<a color='red'>"+data.text+"</div>";
        }else{
            output = data.text;
        }
        $("#subscribe-result").html(output);
    }
});
});
</script>

PHP新闻代码:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

//check if its an ajax request, exit if not
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    //exit script outputting json data
    $output = json_encode(
            array(
                'type' => 'error',
                'text' => 'Request must come from Ajax'
    ));

    die($output);
}

//check $_POST vars are set, exit if any missing
if ( !isset($_POST["useremail"]) ) {
    $output = json_encode(array('type' => 'error', 'text' => 'Input fields are empty!'));
    die($output);
}

//Sanitize input data using PHP filter_var().
$useremail = filter_var(trim($_POST["useremail"]), FILTER_SANITIZE_EMAIL);

//additional php validation

if (!filter_var($useremail, FILTER_VALIDATE_EMAIL)) { //email validation
    $output = json_encode(array('type' => 'error', 'text' => 'Please enter a valid email!'));
    die($output);
}
$list_id = '***secret***';
$api_key = '***secret***';


$data_center = substr($api_key,strpos($api_key,'-')+1);

$url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members';

$json = json_encode([
    'email_address' => $email,
    'status'        => 'subscribed', //pass 'subscribed' or 'pending'
]);


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


if ($status_code!=200) {
    $output = json_encode(array('type' => 'error', 'text' => 'Something Went Wrong'));
    die($output);
} else {
    $output = json_encode(array('type' => 'message', 'text' => 'Hi ' . $username . ' Thank you for your email'));
    die($output);
}





?>
  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 GEO下载数据的处理报错 :函数‘Meta’标签‘"data.frame"’找不到继承方法,如何解决?
      • ¥15 DLNM模型是否可以用二分类变量作为y变量
      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题