From 1c9d0a417f16e81460dc096dc1ad80bb040026eb Mon Sep 17 00:00:00 2001 From: Paulo Reis Date: Tue, 26 Jul 2016 11:30:42 +0100 Subject: [PATCH] Forge a form submission to avoid CORS checking with wufoo. --- .../coffee/services/log-hints-feedback.coffee | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/services/web/public/coffee/services/log-hints-feedback.coffee b/services/web/public/coffee/services/log-hints-feedback.coffee index 7ed383ebd3..0c22f6d44b 100644 --- a/services/web/public/coffee/services/log-hints-feedback.coffee +++ b/services/web/public/coffee/services/log-hints-feedback.coffee @@ -2,12 +2,12 @@ define [ "base" ], (App) -> 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 = DIDNT_UNDERSTAND: "didnt_understand" @@ -16,25 +16,34 @@ define [ OTHER: "other" 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 = method: 'POST' 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 submitFeedback = (hintId, feedbackOpt, feedbackOtherVal = "") -> submitRequest = createRequest hintId, feedbackOpt, feedbackOtherVal - - submitFeedback.then (response) -> + $http(submitRequest).then (response) -> console.log response service =