Azure Search and CORS issue with PUT

2019-03-04 02:35发布

问题:

This is a follow-up issue with my first post. I have successfully deployed my AngularJS web site to Azure and I can get interact with Azure Search using the REST API GET method. Now I am facing the issue with a PUT method. It is complaining that my preflight request does not pass control check. By examining the network traffic, I can see that the browser is doing a OPTIONS request method to Azure Search, and the response code is 404 Not Found.

I noted that someone else had faced this problem , and the work around is to use Standard Pricing Tier. I am already on Standard and problem is still there. My Angular Search CORS is already set to "*". And I have tried to modify my client side AngularJS $http code by adding headers options but still not functional.

Any ideas will be appreciated.

Update #2 to answer @Bruce Johnston, I guess I could not paste too much code as a comment block so I will put them here.

This is a $http.get that is operational:

$http.get("https://mywebsibe/indexes/contact1/docs?api-version=2016-09-01",
              {
                headers: {
                  "Accept": "application/json",
                  "api-key": "xxx"
                },
                params: {
                  //api-version: '2016-09-01',
                  queryType:  "full",
                  search: searchString
                }
              }
              )

This is a PUT that is encountering CORS issue:

$http.put("https://mywebsite/indexes/contact1/docs?api-version=2017-11-11",
      {
        headers: {
         "Access-Control-Allow-Credentials": "true",
         "Access-Control-Allow-Origin": "*",
         "Access-Control-Allow-Methods": "POST, GET, OPTIONS, DELETE",
         "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
         "Accept": "application/json",
         "api-key": "xxx"
        },
        data: {
            "value": [
                {
                    "@search.action": "upload",
                    "ContactId": "id1",
                    "FirstName": "New",
                    "LastName": "Guy",
                    "Dob": "1990-01-31",
                    "Gender": "M",
                    "Email": "ng@somewhere.org"
                }
            ]
        }

回答1:

There are a few issues here that prevent this from working.

Firstly, the hostnames don't appear to be correct. They should be fully-qualified domain names suffixed with .search.windows.net, but that doesn't appear to be the case.

Secondly, the PUT request will not work for three reasons:

  1. It returns 404 because the URL is incorrect, assuming you're trying to upload documents to the index. The URL of the Index API is https://<search-service-name>.search.windows.net/indexes/<index-name>/docs/index.
  2. The Index API is accessed via POST, not PUT.
  3. Even if you had the correct URL and HTTP method, the Index API is not supported via CORS. Azure Search only supports query operations via CORS. Administrative operations or operations that modify data are not supported. This is documented here.

If your goal is to upload documents to your index from the browser, you'll need to proxy the request somehow. Usually we recommend calling Azure Search from your own web API/middle tier, since that avoids having to send your api-keys to the browser.