Ajax Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

前端 未结 11 1508
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 16:55

I\'m writing a simple site that takes as input an idiom, and return its meaning(s) and example(s) from Oxford Dictionary. Here\'s my idea:

I send a request to the fol

相关标签:
11条回答
  • 2020-12-02 17:20

    Is your website also on the oxfordlearnersdictionaries.com domain? or your trying to make a call to a domain and the same origin policy is blocking you?

    Unless you have permission to set header via CORS on the oxfordlearnersdictionaries.com domain you may want to look for another approach.

    0 讨论(0)
  • 2020-12-02 17:22

    If your website also on the oxfordlearnersdictionaries.com domain, USE the following into the oxfordlearnersdictionaries.com .htaccess file:

    Header set Access-Control-Allow-Origin "*"

    0 讨论(0)
  • 2020-12-02 17:22

    I think setting your header to Access-Control-Allow-Origin: * would do the trick here. Had the same issue and I resolved it like that.

    0 讨论(0)
  • 2020-12-02 17:28

    JSONP or "JSON with padding" is a communication technique used in JavaScript programs running in web browsers to request data from a server in a different domain, something prohibited by typical web browsers because of the same-origin policy. JSONP takes advantage of the fact that browsers do not enforce the same-origin policy on script tags. Note that for JSONP to work, a server must know how to reply with JSONP-formatted results. JSONP does not work with JSON-formatted results.

    http://en.wikipedia.org/wiki/JSONP

    Good answer on stackoverflow: jQuery AJAX cross domain

       $.ajax({
            type: "GET",
            url: 'http://www.oxfordlearnersdictionaries.com/search/english/direct/',
            data:{q:idiom},
            async:true,
            dataType : 'jsonp',   //you may use jsonp for cross origin request
            crossDomain:true,
            success: function(data, status, xhr) {
                alert(xhr.getResponseHeader('Location'));
            }
        });
    
    0 讨论(0)
  • 2020-12-02 17:29

    I had the same problem when I was working on asp.net Mvc webApi because cors was not enabled. I solved this by enabling cors inside register method of webApiconfig

    First install cors from here then

       public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
    
            var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);
    
    
    
            config.EnableCors();
            // Web API routes
            config.MapHttpAttributeRoutes();
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    
    0 讨论(0)
  • 2020-12-02 17:36

    Add the below code to your .htaccess

    Header set Access-Control-Allow-Origin *

    It works for me.

    Thanks

    0 讨论(0)
提交回复
热议问题