简体   繁体   English

http帖子不适用于我的代码,但适用于邮递员(rest api客户端)

[英]http post not working on my code, but working on postman(rest api client)

Here's the code: 这是代码:

$http({
            method  : 'POST',
            url     : 'mailer.php',
            data    : {fullname: "a", email: "b@c.c", confirmEmail: "a@ab.c", phone: "09", website: "a.c"} ,  // pass in data as strings
        })  

        .success(function(data, status, headers, config) {
            console.log(data);
            console.log(status);
            console.log(headers);
            console.log(config);

            if (!data.success) {
                // if not successful, bind errors to error variables
                console.log('sending email error');
            } else {
                // if successful, bind success message to message
               // $scope.message = data.message;
               console.log('email sent');
            }
        })

I'm new in angular and php so please take it easy on me. 我是angular和php的新手,所以请放轻松。 :) :)

PHP Code: PHP代码:

$mailSend = null;

if (isset($_POST['email'])) {
    $fullname = (isset($_POST['fullname'])) ? $_POST['fullname'] : '';
    $email = (isset($_POST['email'])) ? $_POST['email'] : '';
    $confirmEmail = (isset($_POST['confirmEmail'])) ? $_POST['confirmEmail'] : '';
    $phone = (isset($_POST['phone'])) ? $_POST['phone'] : '';
    $website = (isset($_POST['website'])) ? $_POST['website'] : '';

    if (strlen($fullname) > 0 && strlen($email) > 0) {
        $to = 'adrian@example.com';

        $subject = 'Pricing Request';

        $header  = "From: " . strip_tags($email) . "\r\n";
        $header .= "Reply-To: ". strip_tags($email) . "\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: text/html; charset=UTF-8";

        $content = '<strong>Full Name: </strong>' . $fullname;
        $content .= '<br/><strong>E-mail: </strong>' . $email;
        $content .= '<br/><strong>Phone: </strong>' . $phone;
        $content .= '<br/><strong>Website: </strong>' . $website;


        $mailSend = (mail($to, $subject, $content, $header));
    }
}

Console Ouput: 控制台输出:

200
sending email error 

You never return an object with success property... that's why data.success is false (cause it doesn't exist at all) : 您永远不会返回具有success属性的对象……这就是为什么data.success为false(因为它根本不存在)的原因:

if (!data.success) {
    // if not successful, bind errors to error variables
    console.log('sending email error');
}

But the request seems OK, you even get a 200 response status which mean "It Worked!". 但是该请求似乎还可以,您甚至收到200响应状态,表示“已成功!”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM