Jquery 否';访问控制允许原点';fidler绕过的请求资源错误上存在标头
我需要使用我公司的身份验证服务,它运行在远程服务器上。我一直在尝试使用jQueryAjax对服务(API)进行ajax调用,如下所示Jquery 否';访问控制允许原点';fidler绕过的请求资源错误上存在标头,jquery,ajax,Jquery,Ajax,我需要使用我公司的身份验证服务,它运行在远程服务器上。我一直在尝试使用jQueryAjax对服务(API)进行ajax调用,如下所示 var uri = "http://Company/Authentication/AuthAPI"; $.ajax(uri, { type: "POST", data: JSON.stringify(user), contentType: "ap
var uri = "http://Company/Authentication/AuthAPI";
$.ajax(uri, {
type: "POST",
data: JSON.stringify(user),
contentType: "application/json",
success: function () {
//here reload the page so that the new vacancy will be showed
alert("logged in");
}
});
我在chrome控制台中遇到此错误
请求的资源上不存在“访问控制允许来源”标头
我经历了,和,并且知道我必须将服务器端的Access Control Allow Origin头设置为*或将其设置为我的域
现在,实际的问题是,当我使用fidler访问同一个身份验证服务时,一切似乎都很好,这让我疯狂,请告诉我
编辑:不知何故,这似乎也适用于.NETMVC代码
public UserData AuthenticateUser(User userDetails)
{
try
{
using (var _client = new HttpClient())
{
var result = new UserData();
_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string subUri = Utilities.GetConfigValueFromSection(ConfigurationConstants.AuthenticationServiceSection, ConfigurationConstants.Authenticate);
var postTask = _client.PostAsJsonAsync(_uri + subUri, userDetails).ContinueWith((taskWithResponse) =>
{
var response = taskWithResponse.Result;
var readTask = response.Content.ReadAsAsync<UserData>();
readTask.Wait();
result = readTask.Result;
});
postTask.Wait();
return result;
}
}
public UserData AuthenticateUser(用户详细信息)
{
尝试
{
使用(var _client=new HttpClient())
{
var result=new UserData();
_client.DefaultRequestHeaders.Accept.Add(新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“应用程序/json”);
string subUri=Utilities.GetConfigValueFromSection(ConfigurationConstants.AuthenticationServiceSection,ConfigurationConstants.Authenticate);
var poststask=\u client.postsjsonasync(\u uri+subUri,userDetails)。ContinueWith((taskWithResponse)=>
{
var response=taskWithResponse.Result;
var readTask=response.Content.ReadAsAsync();
readTask.Wait();
result=readTask.result;
});
postTask.Wait();
返回结果;
}
}
这是为了测试:
此命令禁用web安全性,您可以访问跨域站点。请参阅,所有RESTClient都允许使用此命令,但对于要跨域获取数据的javascript代码,您必须启用CORS 另一种方法是在服务器上创建一个web代理,并调用该代理从该url获取数据,然后返回给您 此处可能存在语法错误:
$.ajax(uri{
将其更改为:
$.ajax({
url: uri
尽管如此,我还是建议您调用您在上面添加的本地方法。该场景被Javascript阻止。您可以在iframe中加载内容,即使头中不存在访问控制允许来源。fiddler可能正在做的事情是相同的。在iframe中加载内容,然后使用e内容。多亏了Jai,我才知道我该做什么……最后很简单 这是解决方案的确切代码 步骤1:angular服务,它使用内置的$http服务对我的web api进行AJAX调用 这里我的web api是ProxyController
$http.post('/Proxy', user).then(function (response) {
successCallBack(response.data); //this is callback function
//this function handles the data returned by the API
}, function (response) {
console.log(response);
});
第2步:代理控制器实施。
这是充当代理服务器的web api
public class ProxyController : ApiController
{
private string AuthenticationUri = "http://authencationserver/api/AuthenticateUser/Authenticate";
public async Task<AuthenticationResultObject> Post(UserObject user)
{
// this controller just acts as an interface between the client and the actual server.
//We are going to get the information from the actual server by making an ajax call from here
//the server address for authenticating is http://authencationserver/api/AuthenticateUser/Authenticate
//we need to hit this address with an user object
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsJsonAsync(AuthenticationUri, user);
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync();
var returnedObject = JsonConvert.DeserializeObject<AuthenticationResultObject>(result.Result);
//we need the above statement to deserialize the returned json to a javascript object.
//AuthenticationResultObject models the data that will be returned by the remote API
return returnedObject;
}
return null;
}
}
公共类代理控制器:ApiController
{
私有字符串身份验证URI=”http://authencationserver/api/AuthenticateUser/Authenticate";
公共异步任务Post(用户对象用户)
{
//该控制器只是作为客户端和实际服务器之间的接口。
//我们将通过在这里进行ajax调用从实际服务器获取信息
//用于身份验证的服务器地址为http://authencationserver/api/AuthenticateUser/Authenticate
//我们需要用一个用户对象点击这个地址
HttpClient=新的HttpClient();
HttpResponseMessage response=wait client.postsjsonasync(AuthenticationUri,用户);
if(响应。IsSuccessStatusCode)
{
var result=response.Content.ReadAsStringAsync();
var returnedObject=JsonConvert.DeserializeObject(result.result);
//我们需要上面的语句将返回的json反序列化为javascript对象。
//AuthenticationResultObject为远程API返回的数据建模
返回返回对象;
}
返回null;
}
}
你正在进行一个跨域ajax调用,这不允许你这么做显示来自fiddler的请求你说fiddler是一个rest客户端?@horrableguy不是rest客户端,但它的工作方式与rest客户端类似,因为它注意到请求被发出,如果你想用postman
对其进行测试,这也会得到。如何?用jax?最后一条评论我想知道什么是响应json/jsonp
?@horrableguy是的,你的编辑和你说的webproxy一样,你可以调用它来为你提供响应。