【发布时间】:2016-04-09 16:13:46
【问题描述】:
我在asp.net中使用java rest webservices,在asp.net中调用服务时我使用ajax调用webservice,下面是我使用的代码
$.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
});
当我运行它时,我得到如下错误提示
XMLHttpRequest 无法加载 http://localhost:9090/EmployeeService/employee/create。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:64992' 因此不允许访问。
我在这个主题上搜索了很多答案,说我必须向单个请求添加自定义标头(或标头集)并显示如下代码
$.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 非常陌生。
【问题讨论】:
标签: c# asp.net ajax web-services