Przeglądaj źródła

Forge a form submission to avoid CORS checking with wufoo.

Paulo Reis 10 lat temu
rodzic
commit
1c9d0a417f

+ 26 - 17
services/web/public/coffee/services/log-hints-feedback.coffee

@@ -2,12 +2,12 @@ define [
 	"base"
 	"base"
 ], (App) ->
 ], (App) ->
 	App.factory "logHintsFeedback", ($http, $q) ->
 	App.factory "logHintsFeedback", ($http, $q) ->
-		if window.sharelatex?.wufoo? and window.sharelatex.wufoo?.token? and window.sharelatex.wufoo?.url?
-			hintsFeedbackFormAPIHash = "rl4xgvr1v5t64a"
-			hintFieldAPIId = '3'
-			feedbackFieldAPIId = '1'
-			basicAuthVal = window.btoa "#{ window.sharelatex.wufoo.token }:anypasswilldo"
-			submitEndpoint = "#{ window.sharelatex.wufoo.url }/api/v3/forms/#{ hintsFeedbackFormAPIHash }/entries.json" 
+		hintsFeedbackFormAPIHash = "rl4xgvr1v5t64a"
+		idStampVal = "OPkEWEFHUFAm7hKlraQMhiOXQabafWo8NipRvLT397w="
+		hintFieldAPIId = "3"
+		reasonFieldAPIId = "1"
+		reasonOtherFieldAPIId = "1_other_other"
+		submitEndpoint = "https://sharelatex.wufoo.eu/forms/#{ hintsFeedbackFormAPIHash }/#public"
 
 
 		feedbackOpts =
 		feedbackOpts =
 			DIDNT_UNDERSTAND: "didnt_understand"
 			DIDNT_UNDERSTAND: "didnt_understand"
@@ -16,25 +16,34 @@ define [
 			OTHER: "other"
 			OTHER: "other"
 
 
 		createRequest = (hintId, feedbackOpt, feedbackOtherVal = "") ->
 		createRequest = (hintId, feedbackOpt, feedbackOtherVal = "") ->
+			formData = new FormData()
+
+			formData.append "Field#{ hintFieldAPIId }", hintId
+			formData.append "Field#{ reasonFieldAPIId }", feedbackOpt
+			formData.append "Field#{ reasonOtherFieldAPIId }", feedbackOtherVal
+			formData.append "idstamp", idStampVal
+
 			req =
 			req =
 				method: 'POST'
 				method: 'POST'
 				url: submitEndpoint
 				url: submitEndpoint
-				headers: 
-					Authorization: "Basic #{ basicAuthVal }"
-
-			req.data = {}
-			req.data["Field#{ hintFieldAPIId }"] = hintId
-			req.data["Field#{ hintFieldAPIId }"] = feedbackOpt
-
-			if feedbackOpt == feedbackOpts.OTHER and feedbackOtherVal != ""
-				req.data["Field#{ hintFieldAPIId }"] = feedbackOtherVal
+				# This will effectively disable Angular's default serialization mechanisms,
+				# forcing the XHR to be done with whatever data we provide (in this case,
+				# form data). Without this, Angular will forcefully try to serialize data
+				# to JSON.
+				transformRequest: angular.identity
+				data: formData
+				headers : 
+					# This will tell Angular to use the browser-provided value, which is
+					# computed according to the data being sent (in this case, multipart
+					# form + browser-specific multipart boundary). Without this, Angular
+					# will set JSON.
+					"Content-Type": undefined
 
 
 			return req
 			return req
 
 
 		submitFeedback = (hintId, feedbackOpt, feedbackOtherVal = "") ->
 		submitFeedback = (hintId, feedbackOpt, feedbackOtherVal = "") ->
 			submitRequest = createRequest hintId, feedbackOpt, feedbackOtherVal
 			submitRequest = createRequest hintId, feedbackOpt, feedbackOtherVal
-
-			submitFeedback.then (response) ->
+			$http(submitRequest).then (response) ->
 				console.log response
 				console.log response
 
 
 		service =
 		service =