简体   繁体   中英

Cannot get Ajax CORS request in jquery to work

I have had this exact code working a week ago and suddenly it stopped working. I don't think anything has changed in bing's api, I have checked my account and still have plenty of searches left. I even mimic this exact request in fiddler and it works. Here is my code:

function queryBing(query, start, callback) {
            var requestStr = "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query=%27" + query + "%27&$top=50&$format=json&ImageFilters=%27Size%3aMedium%27&Adult=%27Moderate%27";
            console.log(requestStr);
            console.log(accountKeyEncoded);
            jQuery.support.cors = true;
            $.ajax({
                url: requestStr,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('Authorization', 'Basic ' + accountKeyEncoded);
                },
                context: this,
                dataType: "json",
                type: 'GET',
                error: function(e){
                    e.Message;
                },
                success: callback
            });
        }

And its returning the following error:

OPTIONS https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query=%27te…$top=50&$format=json&ImageFilters=%27Size%3aMedium%27&Adult=%27Moderate%27 Origin http://localhost:81 is not allowed by Access-Control-Allow-Origin. jquery.js:8706
XMLHttpRequest cannot load https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query=%27te…$top=50&$format=json&ImageFilters=%27Size%3aMedium%27&Adult=%27Moderate%27. Origin http://localhost:81 is not allowed by Access-Control-Allow-Origin. 

I have been able to query from my local computer and my website a week ago. I tried looking inside my account settings and i cant find anywhere in Bing where I would ADD my domain.

I had the same problem starting two days ago - as far as I can tell, Microsoft turned off CORS support in their Bing image search responses during the day on November 4. They no longer return an Access-Control-Allow-Origin header of "*". I googled but couldn't find any information from Microsoft about this change. I worked around the problem by moving the http requests to my server.

The Azure Marketplace team has just rolled out a fix to this issue. OPTIONS requests should now again be correctly enabling Ajax CORS requests. If anyone has code which worked before November but is still broken please let us know on this thread so we can help resolve the issue!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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