4 // Submit post on submit
5 // $('#b1').bind('click', function(event){
6 // event.preventDefault();
7 // console.log("form submitted!") // sanity check
11 $('#b1').bind('click', function(event){
12 event.preventDefault();
13 console.log("form submitted!") // sanity check
14 jsondata=document.getElementById("jsondata").value;
15 user_data=document.getElementById("user_slices").value;
16 create_post(jsondata, user_data,1);
18 $('#b2').bind('click', function(event){
19 event.preventDefault();
20 console.log("form submitted!") // sanity check
21 //jsondata=document.getElementById("jsondata").value;
22 //user_data=document.getElementById("user_slices").value;
23 create_post(jsondata, user_data,2);
26 function create_post(jsondata, user_data,b) {
28 $("button").hide(300);
30 $('#but').html('<img src="http://preloaders.net/preloaders/287/Filling%20broken%20ring.gif"> loading...');
31 console.log("create post is working!")
32 console.log(JSON.stringify(jsondata));
33 var obj = jQuery.parseJSON(jsondata);
34 console.log(JSON.stringify(obj));
36 var myEvent = {"resources":[{"type": "Node"},{"type": "Node"}]};
37 console.log(JSON.stringify(myEvent));
38 //jsondata=JSON.parse(JSON.parse(json).jsondata);
39 jsondata=jsondata.replace("\\", "");
42 url : "create_post/", // the endpoint
43 type : "POST", // http method
44 data : {"name" : user_data, "mitsos" : JSON.stringify(obj), "b": b}, // data sent with the post request
45 // handle a successful response
47 //alert("Request completed");
52 success : function(data,status) {
54 console.log(data,status); // log the returned json to the console
56 var obj = JSON.parse(data);
58 for (var i = 0; i < obj.resource_response.resources.length; i++) {
61 input_2 = "<span> Node-"+k+"</span>"+
62 //"<div class='msglabel'>"<div class='msglabel'>+obj.resource_response.resources[0].domain+"<br>"+obj.resource_response.resources[0].name+"</div>"+
63 "<div class='connect'><div class='msglabel'>domain:"+obj.resource_response.resources[i].domain+"<br>name:"+obj.resource_response.resources[i].name+"<br>"+obj.resource_response.resources[i].urn+"</div></div>";
67 console.log("success"); // another sanity check
69 // handle a non-successful response
70 error : function(xhr,errmsg,err) {
71 $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
72 " <a href='#' class='close'>×</a></div>"); // add the error to the dom
73 console.log("papapa" + xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
79 // This function gets cookie with a given name
80 function getCookie(name) {
81 var cookieValue = null;
82 if (document.cookie && document.cookie != '') {
83 var cookies = document.cookie.split(';');
84 for (var i = 0; i < cookies.length; i++) {
85 var cookie = jQuery.trim(cookies[i]);
86 // Does this cookie string begin with the name we want?
87 if (cookie.substring(0, name.length + 1) == (name + '=')) {
88 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
95 var csrftoken = getCookie('csrftoken');
98 The functions below will create a header with csrftoken
101 function csrfSafeMethod(method) {
102 // these HTTP methods do not require CSRF protection
103 return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
105 function sameOrigin(url) {
106 // test that a given url is a same-origin URL
107 // url could be relative or scheme relative or absolute
108 var host = document.location.host; // host + port
109 var protocol = document.location.protocol;
110 var sr_origin = '//' + host;
111 var origin = protocol + sr_origin;
112 // Allow absolute or scheme relative URLs to same origin
113 return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
114 (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
115 // or any other URL that isn't scheme relative or absolute i.e relative.
116 !(/^(\/\/|http:|https:).*/.test(url));
120 beforeSend: function(xhr, settings) {
121 if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
122 // Send the token to same-origin, relative URLs only.
123 // Send the token only if the method warrants CSRF protection
124 // Using the CSRFToken value acquired earlier
125 xhr.setRequestHeader("X-CSRFToken", csrftoken);