首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ionic2邮件黑猩猩API。Angular2没有发布任何东西

Ionic2邮件黑猩猩API。Angular2没有发布任何东西
EN

Stack Overflow用户
提问于 2017-03-01 13:36:21
回答 1查看 114关注 0票数 0

所以问题是,我目前正在用ionic2开发一个移动应用程序,我必须使用Mailchimp进行订阅。我编写了一个php文件来处理API并将其上传到服务器上。API可以很好地处理我在其中写的静态电子邮件,所以它实际上是工作的。在我做完之后,我开始在angular2/ionic2中处理post数据。但我得到

SyntaxError: JSON中位于0位置的意外标记A

错误。

在这段代码中,整个过程都失败了:

代码语言:javascript
复制
createPost(post: {email: string}): Observable<any>{
    const email = JSON.stringify(post);
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:8100/api', email, {
        headers: headers
    }).map(res => {console.log(res.json())});
  }

我知道问题出在网址上,但我不知道为什么。当我在url中使用http://jsonplaceholder.typicode.com/posts时,它会工作。

我的php代码如下所示(我使用MailChimnp.php包装器):

代码语言:javascript
复制
<?php // for MailChimp API v3.0
include('MailChimp.php');  // path to API wrapper downloaded from GitHub
use \DrewM\MailChimp\MailChimp;

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;

$MailChimp = new MailChimp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-usXX');

$list_id = 'xxxxxxxxxx';
$result = $MailChimp->post("lists/$list_id/members", [
                'email_address' => $email,
                'status'        => 'subscribed',
            ]);

print_r($result);

if ($MailChimp->success()) {
    print_r($result); 
} else {
    echo $MailChimp->getLastError();
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-01 13:38:42

您的map函数需要返回数据。你必须解决这个问题:

代码语言:javascript
复制
createPost(post: {email: string}): Observable<any>{
   const email = JSON.stringify(post);
   let headers = new Headers();
   headers.append('Content-Type', 'application/json');
   return this.http.post('http://localhost:8100/api', email, {
      headers: headers
   }).map(res => {
      console.log(res.json())
      return res.json(); //add this line
   });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42533231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档