Commit 2bc68c0f by Prem Sichanugrist

Add path prefix to all AJAX requests

This will handle the case where the site was deployed under a
path prefix.
parent 80ba9bd0
......@@ -60,7 +60,7 @@ describe 'Calculator', ->
@calculator.calculate()
it 'send data to /calculate', ->
expect($.getJSON).toHaveBeenCalledWith '/calculate',
expect($.getWithPrefix).toHaveBeenCalledWith '/calculate',
equation: '1+2'
, jasmine.any(Function)
......
......@@ -16,7 +16,7 @@ describe 'FeedbackForm', ->
$('#feedback_message').val 'This site is really good.'
$('#feedback_button').click()
expect($.post).toHaveBeenCalledWith '/send_feedback', {
expect($.postWithPrefix).toHaveBeenCalledWith '/send_feedback', {
subject: 'Awesome!'
message: 'This site is really good.'
url: window.location.href
......
......@@ -20,5 +20,5 @@ class @Calculator
$('.help').toggleClass 'shown'
calculate: ->
$.getJSON '/calculate', { equation: $('#calculator_input').val() }, (data) ->
$.getWithPrefix '/calculate', { equation: $('#calculator_input').val() }, (data) ->
$('#calculator_output').val(data.result)
class @Courseware
constructor: ->
Courseware.prefix = $("meta[name='path_prefix']").attr('content')
new Navigation
new Calculator
new FeedbackForm
......
......@@ -5,6 +5,6 @@ class @FeedbackForm
subject: $('#feedback_subject').val()
message: $('#feedback_message').val()
url: window.location.href
$.post '/send_feedback', data, ->
$.postWithPrefix '/send_feedback', data, ->
$('#feedback_div').html 'Feedback submitted. Thank you'
,'json'
jQuery.postWithPrefix = (url, data, callback, type) ->
$.post("#{Courseware.prefix}#{url}", data, callback, type)
jQuery.getWithPrefix = (url, data, callback, type) ->
$.get("#{Courseware.prefix}#{url}", data, callback, type)
$ ->
$.ajaxSetup
headers : { 'X-CSRFToken': $.cookie 'csrftoken' }
......@@ -18,8 +24,8 @@ $ ->
element.schematic.update_value()
schematic_value $("#schematic_#{circuit_id}").attr("value")
$.post "/save_circuit/#{circuit_id}", schematic: schematic_value, (data) ->
$.postWithPrefix "/save_circuit/#{circuit_id}", schematic: schematic_value, (data) ->
alert('Saved') if data.results == 'success'
window.postJSON = (url, data, callback) ->
$.post url, data, callback
$.postWithPrefix url, data, callback
class @Logger
@log: (event_type, data) ->
$.getJSON '/event',
$.getWithPrefix '/event',
event_type: event_type
event: JSON.stringify(data)
page: window.location.href
......@@ -8,7 +8,7 @@ class @Logger
@bind: ->
window.onunload = ->
$.ajax
url: '/event'
url: "#{Courseware.prefix}/event"
data:
event_type: 'page_close'
event: ''
......
......@@ -25,7 +25,7 @@ class @Problem
check: =>
Logger.log 'problem_check', @answers
$.post "/modx/problem/#{@id}/problem_check", @answers, (response) =>
$.postWithPrefix "/modx/problem/#{@id}/problem_check", @answers, (response) =>
switch response.success
when 'incorrect', 'correct'
@render(response.contents)
......@@ -34,18 +34,18 @@ class @Problem
reset: =>
Logger.log 'problem_reset', @answers
$.post "/modx/problem/#{@id}/problem_reset", id: @id, (content) =>
$.postWithPrefix "/modx/problem/#{@id}/problem_reset", id: @id, (content) =>
@render(content)
show: =>
Logger.log 'problem_show', problem: @id
$.post "/modx/problem/#{@id}/problem_show", (response) =>
$.postWithPrefix "/modx/problem/#{@id}/problem_show", (response) =>
$.each response, (key, value) =>
@$("#answer_#{key}").text(value)
save: =>
Logger.log 'problem_save', @answers
$.post "/modx/problem/#{@id}/problem_save", @answers, (response) =>
$.postWithPrefix "/modx/problem/#{@id}/problem_save", @answers, (response) =>
if response.success
alert 'Saved'
......
......@@ -36,7 +36,7 @@ class @Sequence
render: (new_position) ->
if @position != undefined
@mark_visited @position
$.post "/modx/#{@tag}/#{@id}/goto_position", position: new_position
$.postWithPrefix "/modx/#{@tag}/#{@id}/goto_position", position: new_position
if @position != new_position
@mark_active new_position
......
......@@ -45,7 +45,7 @@ class VideoCaption
.append($('<li class="spacing">').height(@bottomSpacingHeight()))
fetchCaption: ->
$.getJSON @captionURL(), (captions) =>
$.getWithPrefix @captionURL(), (captions) =>
@captions = captions.text
@start = captions.start
for index in [0...captions.start.length]
......
......@@ -28,6 +28,7 @@
It can't be run through static.url because MathJax uses crazy url introspection to do lazy loading of
MathJax extension libraries -->
<script type="text/javascript" src="/static/js/mathjax-MathJax-c9db6ac/MathJax.js?config=TeX-AMS_HTML-full"></script>
<meta name="path_prefix" content="${MITX_ROOT_URL}">
</head>
<body class="<%block name='bodyclass'/>">
......
......@@ -75,7 +75,7 @@ $(function() {
});
$('#pwd_reset_button').click(function() {
$.post('/password_reset/',{ "csrfmiddlewaretoken" : "${ csrf }",
$.postWithPrefix('/password_reset/',{ "csrfmiddlewaretoken" : "${ csrf }",
"email" : $('#id_email').val()}, function(data){
$("#password_reset_complete_link").click();
log_event("profile", {"type":"password_send"});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment