如何解决 ionic3 时从 android 和 ios 构建请求的跨源请求问题。我可以使用浏览器中的 ionic serve 访问相同的 en 点,但是当我尝试进行生产构建并访问端点时,我遇到了上述错误。
此外,我什至尝试了 ssl 服务器测试检查我在哪里收到“评估失败:不支持安全协议(protocol)”。我不知道如何解决这个问题。有没有人面临同样的问题?或者知道如何解决这个问题......
我已经导入了
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
并设置标题
headers = new HttpHeaders().set('Content-Type', "application/json");
return new Promise((resolve, reject) => {
this.http.post(TASK.BASEURL + task, requestData, { headers: headers, params: extraParams })
.map(res => res)
.subscribe(response => {
resolve(response);
}, error => {
reject(error)
});
});
------------------------------------错误日志-------- ------------------------
11-27 18:09:55.919 5813-10986/? D/NetworkSecurityConfig:使用来自资源 network_security_config debugBuild 的网络安全配置:false 11-27 18:09:55.985 10808-10808/io.ionic.starter D/SystemWebChromeClient: http://localhost:8080/#/login :第 0 行:访问位于“http://www.xxxxxxxxxxxxx.com/api/login”的 XMLHttpRequest '来自原产地' http://localhost:8080 '已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:预检请求不允许重定向。 11-27 18:09:55.985 10808-10808/io.ionic.starter I/chromium: [INFO:CONSOLE(0)] “从来源‘http://www.xxxxxxxxxxxxx.com/api/login’访问 XMLHttpRequest 已被阻止CORS 策略:对预检请求的响应未通过访问控制检查:预检请求不允许重定向。”,来源:http://localhost:8080 (0) 11-27 18:09:56.454 7163-7182/? I/Finsky:[1422] com.google.android.finsky.bp.an.run(6):执行器的统计信息:BlockingExecutor com.google.android.finsky.bp.ao@afe5f2f[正在运行,池大小 = 0,事件线程 = 0,排队任务 = 0,完成任务 = 7] [1422] com.google.android.finsky.bp.an.run(6):执行器的统计信息:LightweightExecutor com.google.android.finsky.bp.ao@ca9093c[正在运行,池大小 = 4,事件线程 = 0 , 排队任务 = 0, 完成任务 = 55] 11-27 18:09:56.500 7163-7182/? I/Finsky:[1422] com.google.android.finsky.bp.an.run(6):Executor 的统计数据:bgExecutor com.google.android.finsky.bp.ao@53166c5[Running, pool size = 4,事件线程 = 0,排队任务 = 0,完成任务 = 9]
最佳答案
这是正确的做法
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
sendPostRequest(){
const httpOptions = {
headers: new HttpHeaders({
"Content-Type": "application/json"
}),
params: extraParams
};
return this.http.post("url", bodydata);
}
//call method
callPostmethod(){
sendPostRequest.subscribe((response)=>{
//success
console.log("success"+JSON.stringify(response));
}, (error)=>{
//error
console.log("erorr"+JSON.stringify(error));
})
}
关于ionic-framework - (未知 URL): 0 Unknown Error for android and ios build , 的 ionic3 Http 失败响应这适用于 "ionic serve",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493169/