From 42e89dc5571d6bbf7c81c15b17ad4c743443826c Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sat, 17 Aug 2013 22:41:54 -0500 Subject: [PATCH] Hack around cross-domain POSTing --- www/assets/js/account.js | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/www/assets/js/account.js b/www/assets/js/account.js index 49bc7d4a..5ad2d614 100644 --- a/www/assets/js/account.js +++ b/www/assets/js/account.js @@ -9,6 +9,31 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* + So, it turns out that $.post causes Firefox to use a GET request + on cross-site requests. What the hell? I'd understand if they just + made it error instead, but why give me chicken tenders if I ordered a + cheeseburger and act like everything's peachy? +*/ +function postJSON(url, data, callback) { + $.ajax(url, { + method: "POST", + crossDomain: true, + data: data, + success: function (data) { + try { + data = data.substring(data.indexOf("{")); + data = data.substring(0, data.lastIndexOf("}") + 1); + data = JSON.parse(data); + callback(data); + } catch(e) { + return; + } + }, + dataType: "text" + }); +} + var uname = readCookie("cytube_uname") || ""; var session = readCookie("cytube_session") || ""; var loggedin = false; @@ -18,7 +43,7 @@ if(uname && session) { name: uname, session: session }; - $.post(WEB_URL + "/api/login?callback=?", data, function (data) { + postJSON(WEB_URL + "/api/login?callback=?", data, function (data) { if(data.success) onLogin(); }, "jsonp"); @@ -141,7 +166,7 @@ $("#registerbtn").click(function() { pw: pw }; - $.post(WEB_URL + "/api/register?callback=?", data, function (data) { + postJSON(WEB_URL + "/api/register?callback=?", data, function (data) { if(data.success) { uname = name; session = data.session; @@ -176,7 +201,8 @@ $("#loginbtn").click(function() { name: uname, pw: $("#loginpw").val() }; - $.post(WEB_URL+"/api/login?callback=?", data, function(data) { + + postJSON(WEB_URL+"/api/login?callback=?", data, function(data) { if(data.success) { session = data.session; onLogin(); @@ -238,7 +264,7 @@ $("#cpwbtn").click(function() { oldpw: oldpw, newpw: newpw }; - $.post(WEB_URL + "/api/account/passwordchange?callback=?", data, + postJSON(WEB_URL + "/api/account/passwordchange?callback=?", data, function (data) { if(data.success) { $("
").addClass("alert alert-success") @@ -291,7 +317,7 @@ $("#cebtn").click(function() { pw: pw, email: email }; - $.post(WEB_URL + "/api/account/email?callback=?", data, + postJSON(WEB_URL + "/api/account/email?callback=?", data, function (data) { if(data.success) { $("
").addClass("alert alert-success") @@ -321,7 +347,7 @@ $("#rpbtn").click(function() { name: name, email: email }; - $.post(WEB_URL + "/api/account/passwordreset?callback=?", data, + postJSON(WEB_URL + "/api/account/passwordreset?callback=?", data, function (data) { $("#rpbtn").text("Send Reset"); if(data.success) { @@ -350,7 +376,7 @@ $("#profilesave").click(function() { profile_text: text }; - $.post(WEB_URL+"/api/account/profile?callback=?", data, + postJSON(WEB_URL+"/api/account/profile?callback=?", data, function (data) { if(data.success) { $("
").addClass("alert alert-success")