对预检请求的响应未通过访问控制检查:否'Access-Control-Allow-Origin' [英] Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

查看:116
本文介绍了对预检请求的响应未通过访问控制检查:否'Access-Control-Allow-Origin'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我在asp.net中使用java rest webservices,而在asp.net中调用service时我正在使用ajax来调用webservice,下面是我使用的代码

Here i am consuming java rest webservices in asp.net,while calling service in asp.net i am using ajax to call webservice,below is the code i have used

 $.ajax({
            type: "POST",
            url: 'http://localhost:9090/EmployeeService/employee/create',


            data: { id: 4, first_name:"narayan", last_name:"phone", email:"phone", phone:4545454545 },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccessCall,
            error: OnErrorCall
        });

运行此命令时,出现如下错误提示

when i run this i get error saying as below

XMLHttpRequest无法加载 http://localhost:9090/EmployeeService/employee/create .对预检请求的响应未通过访问控制检查:在所请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许访问来源' http://localhost:64992 .

XMLHttpRequest cannot load http://localhost:9090/EmployeeService/employee/create. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:64992' is therefore not allowed access.

我在这个主题上进行了搜索,发现了很多答案,说我必须向单个请求添加自定义标头(或标头集),并向我显示以下代码

i googled on this topic i found many answers saying i have to add a custom header (or set of headers) to an individual request and shows me code as below

$.ajax({
url: 'foo/bar',
headers: { 'x-my-custom-header': 'some value' }
});

$.ajaxSetup({
headers: { 'x-my-custom-header': 'some value' }
});

// Sends your custom header
$.ajax({ url: 'foo/bar' });

// Overwrites the default header with a new header
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' }     }); 

在这里,根据我的代码,我应该用什么代替"x-some-other-header"和一些值".这是解决此错误的正确方法还是其他解决方案?请指导我对asp.net来说是非常新的.

here what should i substitute for 'x-some-other-header' and "some value" according to my code.Is this the right way to solve this error or what is the other solution?.Please guide me i am very new to asp.net.

推荐答案

我通过在服务器端提供对跨域的访问来解决它.由于该服务是使用maven用Java编写的,因此我通过添加添加使其支持跨域web.xml页面上的这些过滤器

I solved it by giving access to cross-domain in server side.As the service was written in java using maven i made it to support cross domain by adding in web.xml page these filters

      <filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
     </filter>
  <filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

通过将这些添加到web.xml中,我能够解决该错误.

By adding these in web.xml,i was able to solve the error.

这篇关于对预检请求的响应未通过访问控制检查:否'Access-Control-Allow-Origin'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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