[英]Unexpected Response From Get Request Angular 2
背景
Angular 2應用程序正在本地主機上使用PHP API。 我知道我的URL端點設置正確,因為我是用Postman檢查的。 當我發出get請求時,響應就是我自己頁面的html。 我收到成功代碼200,但響應錯誤,因為
錯誤
例外:JSON中位置0處的意外令牌<
所以當我console.log(res)我得到這個,
//我從角度2前端獲取的HTML。
"<!DOCTYPE html>↵<html>↵<head> etc...
我期待在回應中,
{
"error": true,
"api_key": false,
"message": "User is registered in our database now use update api to fill other details"
}
題
是什么會導致響應從發出初始請求的頁面保存我自己的html?
代碼示例
我的要求
public checkRegister(model: LogReg) {
// Parameters obj-
let params: URLSearchParams = new URLSearchParams();
params.set('email', model.email);
params.set('email_verified', model.email_verified);
//Http request-
return this.http.get(STATICS.API_BASE + STATICS.API_LOGIN,
{ search: params }).map((res:Response) => res.json());
}
這就是我處理回復的方式,
// Login Or Register User On Our Server
this.logreg = new LogReg(profile.email_verified, profile.email);
this.checkRegister(this.logreg).subscribe((res)=>{
console.log(res);
if (profile.email_verified === false) {
console.log(res);
console.log("Check your email to verify your account!");
this.logout(this.user);
}
else if (res.profile_exist === false) {
console.log(res);
this.router.navigateByUrl('/profile');
} else if (res.profile_exist === true) {
console.log(res);
this.router.navigateByUrl('/overview');
}
});
res.json是導致您看到要回答問題的錯誤的原因。
res.json試圖解析響應的有效負載,而控制台判斷該響應的負載根本不是json。 您可以將res.json包裹在try catch中,以防止錯誤破壞您的應用程序前進。
您可能希望在開發工具中打開“網絡”選項卡時觸發獲取請求,並確認您的請求正在發送期望的內容。 我也懷疑如果您要打一個API調用來檢查有效負載,則您想發送一個發布請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.