所以问题是,我目前正在用ionic2开发一个移动应用程序,我必须使用Mailchimp进行订阅。我编写了一个php文件来处理API并将其上传到服务器上。API可以很好地处理我在其中写的静态电子邮件,所以它实际上是工作的。在我做完之后,我开始在angular2/ionic2中处理post数据。但我得到
SyntaxError: JSON中位于0位置的意外标记A
错误。
在这段代码中,整个过程都失败了:
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包装器):
<?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();
}发布于 2017-03-01 13:38:42
您的map函数需要返回数据。你必须解决这个问题:
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
});
}https://stackoverflow.com/questions/42533231
复制相似问题