nivturk diff

Created Diff never expires
4 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
88 lines
29 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
111 lines
// Pass message from jsPsych to NivTurk
// Pass message from jsPsych to NivTurk
function pass_message(experiment, msg) {
function pass_message(experiment, msg) {


$.ajax({
$.ajax({
url: "/experiment?experiment=" + experiment,
url: "/experiment?experiment=" + experiment,
method: 'POST',
method: 'POST',
data: JSON.stringify(msg),
data: JSON.stringify(msg),
contentType: "application/json; charset=utf-8",
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
}).done(function(data, textStatus, jqXHR) {
// do nothing on success
// do nothing on success
}).fail(function(error) {
}).fail(function(error) {
console.log(error);
console.log(error);
});
});


}
}


// Successful completion of experiment: redirect to experiment page.
// // Successful completion of experiment: redirect to experiment page.
// function on_success(experiment) {
// $.ajax({
// url: "/on_success?experiment=" + experiment,
// method: 'POST',
// data: JSON.stringify(jsPsych.data.get().json()),
// contentType: "application/json; charset=utf-8",
// }).done(function(data, textStatus, jqXHR) {
// console.log("AJAX success, redirecting to /main");
// window.location.replace('/main'); // back to home
// }).fail(function(error) {
// console.log(error);
// });

// }

function on_success(experiment) {
function on_success(experiment) {
console.log("on_success function called with experiment:", experiment);
console.log("on_success function called with experiment:", experiment);
const payload = {
experiment: experiment,
data: jsPsych.data.get().json()
};


$.ajax({
$.ajax({
url: "/on_success?experiment=" + experiment,
url: "/on_success",
method: 'POST',
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
data: JSON.stringify(payload),
contentType: "application/json; charset=utf-8",
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
}).done(function(data, textStatus, jqXHR) {
console.log("AJAX success, redirecting to /main");
window.location.replace('/main');
window.location.replace('/main');
}).fail(function(error) {
}).fail(function(error) {
console.log(error);
console.log("AJAX request failed:", error);
});
});

}
}



// Successful completion of all experiments.
// Successful completion of all experiments.
function redirect_success() {
function redirect_success() {
window.location.replace('/redirect_success');
window.location.replace('/redirect_success');
}
}


// Unsuccessful completion of experiment: redirect with decoy code.
// Unsuccessful completion of experiment: redirect with decoy code.
function redirect_reject(error) {
function redirect_reject(error) {


// Concatenate metadata into complete URL (returned on reject).
// Concatenate metadata into complete URL (returned on reject).
var url = "/error/" + error;
var url = "/error/" + error;


$.ajax({
$.ajax({
url: "/redirect_reject",
url: "/redirect_reject",
method: 'POST',
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
}).done(function(data, textStatus, jqXHR) {
window.location.replace(url);
window.location.replace(url);
}).fail(function(error) {
}).fail(function(error) {
console.log(error);
console.log(error);
});
});
}
}


// Unsuccessful completion of experiment: redirect to error page.
// Unsuccessful completion of experiment: redirect to error page.
function redirect_error(error) {
function redirect_error(error) {


// error is the error number to redirect to.
// error is the error number to redirect to.
var url = "/error/" + error;
var url = "/error/" + error;


$.ajax({
$.ajax({
url: "/redirect_error",
url: "/redirect_error",
method: 'POST',
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
}).done(function(data, textStatus, jqXHR) {
window.location.replace(url);
window.location.replace(url);
}).fail(function(error) {
}).fail(function(error) {
console.log(error);
console.log(error);
});
});
}
}


// Save data at regular intervals during experiment without redirecting to another page
// Save data at regular intervals during experiment without redirecting to another page
//Adapted from functions above and https://towardsdatascience.com/using-python-flask-and-ajax-to-pass-information-between-the-client-and-server-90670c64d688
//Adapted from functions above and https://towardsdatascience.com/using-python-flask-and-ajax-to-pass-information-between-the-client-and-server-90670c64d688
function interval_save(task){
function interval_save(task){
$.ajax({
$.ajax({
url: "/experiments/" + task,
url: "/experiments/" + task,
method: 'POST',
method: 'POST',
data: JSON.stringify(jsPsych.data.get().json()),
data: JSON.stringify(jsPsych.data.get().json()),
contentType: "application/json; charset=utf-8",
contentType: "application/json; charset=utf-8",
}).done(function(data, textStatus, jqXHR) {
}).done(function(data, textStatus, jqXHR) {
console.log("interval_save done");
console.log("interval_save done");
}).fail(function() {
}).fail(function() {
console.log("interval_save error");
console.log("interval_save error");
});
});
}
}