简体   繁体   English

Ajax-'Origin localhost不允许Access-Control-Allow-Origin'

[英]Ajax - 'Origin localhost is not allowed by Access-Control-Allow-Origin'

I'm relatively new to Ajax and was just tasked with this cross-domain call. 我是Ajax的新手,只是受过此跨域调用的任务。 We have a text box on our web page that a user will use to preform a search of company names. 我们的网页上有一个文本框,用户将使用该文本框执行公司名称搜索。 By clicking a button next to the text box, the Ajax call will be requested. 通过单击文本框旁边的按钮,将请求Ajax调用。 Unfortunately the web service is located in a separate domain, so this is naturally causing issues. 不幸的是,Web服务位于单独的域中,因此自然会引起问题。

Below is my best attempt at making this work. 以下是我使这项工作的最佳尝试。 I should also note, the purpose of this call is to return the results in an XML format, which will be parsed in the success portion of the request. 我还要注意,此调用的目的是以XML格式返回结果,该结果将在请求的success部分中进行解析。

Here is the error message again: 这又是错误消息:

Origin http://localhost:55152 is not allowed by Access-Control-Allow-Origin.

I'm at a loss as to what to do for a work-around, any ideas would be greatly appreciated. 对于解决方法,我不知所措,任何想法将不胜感激。

function GetProgramDetails() {
    var URL = "http://quahildy01/xRMDRMA02/xrmservices/2011/OrganizationData.svc/AccountSet?$select=AccountId,Name,neu_UniqueId&$filter=startswith(Name,\'" + $('.searchbox').val() + "\')";
    var request = $.ajax({
        type: 'POST',
        url: URL,
        contentType: "application/x-www-form-urlencoded",
        crossDomain: true,
        dataType: XMLHttpRequest,
        success: function (data) {
            console.log(data);
            alert(data);
        },
        error: function (data) {
            console.log(data);
            alert("Unable to process your resquest at this time.");
        }
    });
}

This error is due to the restriction enforced in cross-domain resource sharing. 此错误是由于跨域资源共享中实施的限制所致。 This has been implemented as a part of security feature to restrict the clients(domain) of a resource via cross domain calls. 这已作为安全功能的一部分实现,以通过跨域调用限制资源的客户端(域)。 When you send a request to the webservice or api or similar, it adds Origin header in the request for the server or destination (here your api) to validate if the request is coming from an authorized source or not. 当您将请求发送到Web服务或api或类似工具时,它会在服务器或目标(此处是api)的请求中添加Origin标头,以验证请求是否来自授权来源。 Ideally the api/server should look for the Origin in the Request header it received and probably validate against the set of origins(domains) which it is permitted to serve the resources to. 理想情况下,api /服务器应在接收到的Request header查找Origin ,并可能针对允许向其提供资源的原始域集进行验证。 If it is coming from a permitted domain it will add the same domain in the response header as "Access-Control-Allow-Origin" value. 如果来自允许的域,它将在响应标头中添加与"Access-Control-Allow-Origin"值相同的域。 wildcard is also permitted for this, but the issue is that with wild card permission any one can make a request and get it served (with some restrictions like an api is authenticated via windows auth or cookies where you need to send the withCredentials value * is not allowed). 也可以使用通配符,但是问题是,通过通配符许可,任何人都可以发出请求并将其送达(有一些限制,例如通过Windows auth或cookie对api进行身份验证,而您需要发送withCredentials*是不允许)。 it is not a good practice to use wildcard origin the response header which makes it open to everyone. 使用通配符来源的响应标头不是一个好习惯,因为它对所有人开放。

These are some ways to set the response header with the values:- 这些是使用值设置响应头的方法:-

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: http://yourdomain.com

you can even add multiple Access-Control-Allow-Origin headers in the same response (I believe works in most browsers) 您甚至可以在同一响应中添加多个Access-Control-Allow-Origin标头(我相信在大多数浏览器中都可以使用)

Access-Control-Allow-Origin: http://yourdomain1.com
Access-Control-Allow-Origin: http://yourdomain2.com
Access-Control-Allow-Origin: http://yourdomain3.com

On the server side (c# syntax) you would do this:- 在服务器端(c#语法),您可以这样做:

var sourceDomain = Request.Headers["Origin"]; //This gives the origin domain for the request
     Response.AppendHeader("Access-Control-Allow-Origin", sourceDomain ); //Set the response header with the origin value after validation (if any) .Depending on the type of application you are using syntax may vary.

Hope this helps!!! 希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM