Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios ionic 2 post请求不起作用_Ios_Ionic Framework_Ionic2 - Fatal编程技术网

Ios ionic 2 post请求不起作用

Ios ionic 2 post请求不起作用,ios,ionic-framework,ionic2,Ios,Ionic Framework,Ionic2,我正在使用下面的代码来处理离子2中的post方法请求 var headers = new Headers(); headers.append("Accept", 'application/json'); headers.append('Content-Type', 'application/json'); headers.append('Access-Control-Allow-Origin', '*'); headers.append('Access-Control-Allow-Headers

我正在使用下面的代码来处理离子2中的post方法请求

var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Headers', 'Content-Type');
headers.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');

let body = new FormData();
body.append('UserName', 'sp');
body.append('password', 's');

let options = new RequestOptions({ headers: headers })

this.http
    .post('http://192.168.1.9:8080/api/Restaurant/Login', body, options)
    .map(res => res.json())
    .subscribe(
        data => {
          console.log("Response is ::::::",data);
        },
          err => {
          console.log("ERROR!::::: ", err);
        }
    );
上面是我的本地URL。有了这个URL,我可以从iOS模拟器和邮递员那里得到请求/响应,但不能从ionic 2项目中得到

我得到的错误,根据所附的屏幕截图

我们在下面添加了CORS头的服务器端

<httpprotocol>
    <customheaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
        <add name="Access-Control-Allow-Credentials" value="true" />
    </customheaders>
</httpprotocol>

在我错的地方谁能帮忙


提前感谢。

在服务器端,您是否配置了Web安全性?我对angular的经验是,它在发送实际请求之前发送飞行前选项请求,如果您的服务器不支持选项请求,那么它将拒绝它。 我不确定这是否会对你有帮助,因为它是在春天拍摄的

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.....

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
        .and()
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .authorizeRequests()
        .antMatchers("/**/user").permitAll()
        ...
        .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() //This allows options requests
        ...

}
祝你一切顺利!希望你能成功:)
快乐编码

headers.append('Access-Control-Allow-Origin','*');headers.append('Access-Control-Allow-headers','Content-Type');headers.append('Access-Control-Allow-Methods','GET、PUT、POST、DELETE、OPTIONS')是服务器端头。。你不需要在爱奥尼亚设置app@suraj在服务器端,所有的头都被添加。如果我从ionic应用程序中删除这些标题,那么它也不起作用@hrdkisback无法使用此正文。您在服务器端的接收情况如何?作为json?