NativeScript Angular 上的 Http 请求失败 [英] Http request fail on NativeScript Angular

查看:27
本文介绍了NativeScript Angular 上的 Http 请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有杂货教程,正在使用.然后我转到用户服务并更改登录的 URL:

I have the Groceries tutorial, working. Then I go to the user service and change the URL of the login:

login(user: User) {
let headers = new Headers();
headers.append("Content-Type", "application/json");

return this.http.post(
  "http://ws.u-vox.com/api/noauth/loginvenue",
  JSON.stringify({
    username: user.email,
    password: user.password,
  }),
  { headers: headers }
)
.map(response => response.json())
.do(data => {
  Config.token = data.Result.access_token;
})
.catch(this.handleErrors);
}

handleErrors(error: Response) {
console.log(JSON.stringify(error.json()));
return Observable.throw(error);
}

当我按下登录时,我希望终端上有一条 http 消息和一个 JSON.但相反,我变得一团糟,我不知道发生了什么.

When I press sign in I would expect a http message and a JSON on the terminal. But instead I'm getting this mess, and I don't know what is happening.

CONSOLE LOG file:///app/shared/user/user.service.js:38:20: {"line":993,"column":38,"sourceURL":"file:///app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js","originalStack":"ZoneAwareError@file:///app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:993:38\nfile:///app/tns_modules/tns-core-modules/http/http-request/http-request.js:86:37\nUIApplicationMain@[native code]\nstart@file:///app/tns_modules/tns-core-modules/application/application.js:211:26\nbootstrapApp@file:///app/tns_modules/nativescript-angular/platform-common.js:72:28\nbootstrapModule@file:///app/tns_modules/nativescript-angular/platform-common.js:60:26\nanonymous@file:///app/main.js:7:57\nevaluate@[native code]\nmoduleEvaluation@[native code]\n[native code]\npromiseReactionJob@[native code]","zoneAwareStack":"file:///app/tns_modules/tns-core-modules/http/http-request/http-request.js:86:37 [<root>]\nUIApplicationMain@[native code] [<root>]\nstart@file:///app/tns_modules/tns-core-modules/application/applicati
CONSOLE ERROR file:///app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:569:26: Unhandled Promise rejection: Animation cancelled. ; Zone: <root> ; Task: null ; Value: Error: Animation cancelled. _rejectAnimationFinishedPromise@file:///app/tns_modules/tns-core-modules/ui/animation/animation-common.js:98:31 [<root>]

我的端点在 NativeScript 应用程序之外工作:

My endpoint works outside NativeScript app:

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Cache-Control: no-cache, private
Date: Thu, 28 Dec 2017 15:00:39 GMT
Allow: POST

{"username ... }

推荐答案

我认为这个问题是由 CORS(跨域)请求引起的.

I think this issue is due to CORS (cross domain) request.

首先确保您发出 post 请求的 url 已启用其 CORS.

First make sure the url on which you make the post request have its CORS enabled.

其次,如果您使用 iOS 来运行您的应用程序,请将以下内容添加到 /nativescript/App_Resources/iOS/info.plist:

Second, if you are using iOS to run your app, add the following to /nativescript/App_Resources/iOS/info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <!--Include to allow all connections (DANGER)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

对于 android,我认为有一个等效的设置.

For android I think there is an equivalent setting.

这篇关于NativeScript Angular 上的 Http 请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆