2017-09-13 52 views
0

我正在爲個人項目編寫GWT前端,並且遇到了一些HTTP請求的問題。當我做了CORS POST請求,它工作正常POST GWT CORS請求有效,但PUT CORS請求不會

String url = BASE_URL + "students/"; 

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url); 
builder.setHeader("Content-Type", "application/vnd.onelostlogician.student+json"); 
builder.setHeader("Accept", "application/json"); 
StringBuilder basicAuth = new StringBuilder(); 
basicAuth.append(username.getValue()); 
basicAuth.append(":"); 
basicAuth.append(password.getValue()); 
String basicAuthStr = basicAuth.toString(); 
builder.setHeader("Lambda-Authorization", "Basic " + toBase64(basicAuthStr.getBytes())); 
StudentWriter studentWriter = GWT.create(StudentWriter.class); 

try { 
    builder.sendRequest(studentWriter.write(student), new RequestCallback() { 
     public void onError(Request request, Throwable exception) { 
      addItemDialog.close(); 
      responseDialog.open(); 
      loadingIcon.setVisible(false); 
      responseHeading.setText("No response"); 
      responseLabel.setText(request.toString()); 
     } 

     public void onResponseReceived(Request request, Response response) { 
      loadingIcon.setVisible(false); 
      String responseText = response.getText(); 

      List<Map.Entry<Integer, Student>> students = model.getList(); 
      Integer studentId = Integer.parseInt(responseText); 

      students.add(new AbstractMap.SimpleEntry<>(studentId, student)); 
      model.setList(students); 

      // clear text fields 
      className.setValue(""); 
      additionLevel.setValue(""); 
      additionProblems.setValue(""); 
      subtractionLevel.setValue(""); 
      subtractionProblems.setValue(""); 
      multiplicationLevel.setValue(""); 
      multiplicationProblems.setValue(""); 
      divisionLevel.setValue(""); 
      divisionProblems.setValue(""); 

      addItemDialog.close(); 
     } 
    }); 

} catch (RequestException _) { 
    // Code omitted for clarity 
} 

選項請求得到一個200響應(鉻網下面檢查):

General 
Request URL:https://[redacted].execute-api.eu-west-1.amazonaws.com/v1/students/ 
Request Method:OPTIONS 
Status Code:200 
Remote Address:54.230.9.41:443 
Referrer Policy:no-referrer-when-downgrade 

Response Headers 
access-control-allow-headers:content-type, lambda-authorization 
access-control-allow-methods:post, get, put 
access-control-allow-origin:* 
content-length:0 
content-type:application/json 
date:Wed, 13 Sep 2017 14:56:32 GMT 
status:200 
via:1.1 5db82aafd9021b07695423274288b59e.cloudfront.net (CloudFront) 
x-amz-cf-id:8nJ2gzqHFPiiDOOeEelzkpI7Ga9SFdEcljiLt2pvm7Z995_GicxPVw== 
x-amzn-requestid:bb0e23db-9893-11e7-bbbe-9bea7d9d70bf 
x-amzn-trace-id:sampled=0;root=1-59b94720-d892209d8c5c2a04832bdb85 
x-cache:Miss from cloudfront 

Request Headers 
:authority:[redacted].execute-api.eu-west-1.amazonaws.com 
:method:OPTIONS 
:path:/v1/students/ 
:scheme:https 
accept:*/* 
accept-encoding:gzip, deflate, br 
accept-language:en-GB,en-US;q=0.8,en;q=0.6 
access-control-request-headers:content-type,lambda-authorization 
access-control-request-method:POST 
origin:http://127.0.0.1:8888 
referer:http://127.0.0.1:8888/ArithmeticExerciseGeneratorClient.html 
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 

,然後將POST請求時作爲預期

General 
Request URL:https://[redacted].execute-api.eu-west-1.amazonaws.com/v1/students/ 
Request Method:POST 
Status Code:201 
Remote Address:54.230.9.41:443 
Referrer Policy:no-referrer-when-downgrade 

Response Headers 
access-control-allow-origin:* 
content-length:1 
content-type:application/json 
date:Wed, 13 Sep 2017 14:56:33 GMT 
status:201 
via:1.1 5db82aafd9021b07695423274288b59e.cloudfront.net (CloudFront) 
x-amz-cf-id:gxYrwctM75ObiPyS4nD69jXSO4dBaMAOZmXXX0mPE4wMgCdcjUSQsA== 
x-amzn-requestid:bb381a33-9893-11e7-a1f1-17fd67ca388c 
x-amzn-trace-id:sampled=0;root=1-59b94720-1c1e3a8d8c9ce2741c789241 
x-cache:Miss from cloudfront 

Request Headers 
:authority:[redacted].execute-api.eu-west-1.amazonaws.com 
:method:POST 
:path:/v1/students/ 
:scheme:https 
accept:application/json 
accept-encoding:gzip, deflate, br 
accept-language:en-GB,en-US;q=0.8,en;q=0.6 
content-length:224 
content-type:application/vnd.onelostlogician.student+json 
lambda-authorization:Basic [redacted] 
origin:http://127.0.0.1:8888 
referer:http://127.0.0.1:8888/ArithmeticExerciseGeneratorClient.html 
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 

Request Payload 
{"className":"6T","additionProblemId":4,"additionNoOfProblems":5,"subtractionProblemId":3,"subtractionNoOfProblems":5,"multiplicationProblemId":2,"multiplicationNoOfProblems":5,"divisionProblemId":1,"divisionNoOfProblems":5} 

不幸的是,在同一臺服務器上,對一個非常相似的資源的PUT請求沒有。該代碼幾乎是相同的:

String url = BASE_URL + "students/" + studentId; 

RequestBuilder builder = new RequestBuilder(RequestBuilder.PUT, url); 
builder.setHeader("Content-Type", "application/vnd.onelostlogician.student+json"); 
builder.setHeader("Accept", "application/json"); 
StringBuilder basicAuth = new StringBuilder(); 
basicAuth.append(username.getValue()); 
basicAuth.append(":"); 
basicAuth.append(password.getValue()); 
String basicAuthStr = basicAuth.toString(); 
builder.setHeader("Lambda-Authorization", "Basic " + toBase64(basicAuthStr.getBytes())); 
StudentWriter studentWriter = GWT.create(StudentWriter.class); 

try { 
    builder.sendRequest(studentWriter.write(student), new RequestCallback() { 
     public void onError(Request request, Throwable exception) { 
      addItemDialog.close(); 
      responseDialog.open(); 
      loadingIcon.setVisible(false); 
      responseHeading.setText("No response"); 
      responseLabel.setText(request.toString()); 
     } 

     public void onResponseReceived(Request request, Response response) { 
      loadingIcon.setVisible(false); 
      responseDialog.open(); 
      loadingIcon.setVisible(false); 
      responseHeading.setText("Response: " + response.getStatusCode()); 
      responseLabel.setText(response.getText()); 
     } 
    }); 

} catch (RequestException _) { 
    // Code omitted for clarity 
} 

選項請求得到一個200響應:

General 
Request URL:https://[redacted].execute-api.eu-west-1.amazonaws.com/v1/students/4 
Request Method:OPTIONS 
Status Code:200 
Remote Address:54.230.9.41:443 
Referrer Policy:no-referrer-when-downgrade 

Response Headers 
access-control-allow-headers:content-type, lambda-authorization 
access-control-allow-methods:get, put 
access-control-allow-origin:* 
content-length:0 
content-type:application/json 
date:Wed, 13 Sep 2017 14:58:38 GMT 
status:200 
via:1.1 5db82aafd9021b07695423274288b59e.cloudfront.net (CloudFront) 
x-amz-cf-id:0PoyOa6oDBSmU7iCWZyeSZFqWxZvumN8C4GtHn8rsoJK5AURbj3kxQ== 
x-amzn-requestid:063270d4-9894-11e7-9d66-71b07b2689ef 
x-amzn-trace-id:sampled=0;root=1-59b9479e-39be94b25784b92027fa2753 
x-cache:Miss from cloudfront 

Request Headers 
:authority:[redacted].execute-api.eu-west-1.amazonaws.com 
:method:OPTIONS 
:path:/v1/students/4 
:scheme:https 
accept:*/* 
accept-encoding:gzip, deflate, br 
accept-language:en-GB,en-US;q=0.8,en;q=0.6 
access-control-request-headers:content-type,lambda-authorization 
access-control-request-method:PUT 
origin:http://127.0.0.1:8888 
referer:http://127.0.0.1:8888/ArithmeticExerciseGeneratorClient.html 
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 

...但接收到成功OPTIONS響應後,它不會使PUT要求在所有。

在Chrome控制檯我得到:

的XMLHTTPRequest不能加載https://[redacted]/v1/students/5。方法 PUT沒有被訪問控制允許的方法在預檢 響應允許

我不明白的是錯誤的,因爲我們可以看到「put」在access-control-allow-methods響應頭,預檢OPTIONS請求如上所示。

任何想法我做錯了什麼?

+0

在你說的問題部分,*「不會繼續執行所需的PUT CORS操作」*,請考慮使用https://stackoverflow.com/posts/46202370/edit來編輯/更新問題以明確你的意思嗎?你可能想在你的瀏覽器devtools中打開Network窗格,然後重新加載並查找,然後返回,並在你的問題中指出(1)在OPTIONS之後瀏覽器是否真的不發送PUT請求?還是呢? (2)如果有,服務器爲響應該PUT請求而發送的響應狀態碼(2xx,4xx,5xx?)和標題? – sideshowbarker

+0

編輯澄清。在OPTIONS之後,瀏覽器根本不發送PUT請求。 – user1853665

+0

瀏覽器引擎不記錄任何類型的錯誤信息?對不起,不熟悉GWT故障排除如何工作,但是沒有控制檯在哪裏出錯?如果有,那裏記錄沒有錯誤?你是否能夠在不同的瀏覽器中測試一些版本的代碼?火狐?(看看你是否得到了同樣的沒有PUT請求發送的行爲。) – sideshowbarker

回答

0

在POST響應,所允許的方法報頭是

access-control-allow-methods:post, get, put 

在PUT響應,所允許的方法報頭是

access-control-allow-methods:get, put 

注意,所要求的方法是在列表中的第一POST案例,但在PUT案例的列表中排在第二位。當我修改服務器以便首先在列表中考慮方法時(並且爲了區分大小寫,因爲HTTP方法名稱區分大小寫),瀏覽器會執行所需的後續PUT請求。