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 create_post(jsondata);
18 function create_post(jsondata) {
19 $("button").hide(300);
21 $('#but').html('<img src="http://preloaders.net/preloaders/287/Filling%20broken%20ring.gif"> loading...');
22 console.log("create post is working!")
23 console.log(JSON.stringify(jsondata));
24 var obj = jQuery.parseJSON(jsondata);
25 console.log(JSON.stringify(obj));
27 var myEvent = {"resources":[{"type": "Node"},{"type": "Node"}]};
28 console.log(JSON.stringify(myEvent));
29 //jsondata=JSON.parse(JSON.parse(json).jsondata);
30 jsondata=jsondata.replace("\\", "");
31 console.log(jsondata);
33 url : "create_post/", // the endpoint
34 type : "POST", // http method
35 data : JSON.stringify(obj), // data sent with the post request
36 // handle a successful response
38 //alert("Request completed");
42 success : function(data,status) {
44 console.log(data,status); // log the returned json to the console
46 var obj = JSON.parse(data);
48 for (var i = 0; i < obj.resource_response.resources.length; i++) {
51 input_2 = "<span> Node-"+k+"</span>"+
52 //"<div class='msglabel'>"<div class='msglabel'>+obj.resource_response.resources[0].domain+"<br>"+obj.resource_response.resources[0].name+"</div>"+
53 "<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>";
57 console.log("success"); // another sanity check
59 // handle a non-successful response
60 error : function(xhr,errmsg,err) {
61 $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
62 " <a href='#' class='close'>×</a></div>"); // add the error to the dom
63 console.log("papapa" + xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
69 // This function gets cookie with a given name
70 function getCookie(name) {
71 var cookieValue = null;
72 if (document.cookie && document.cookie != '') {
73 var cookies = document.cookie.split(';');
74 for (var i = 0; i < cookies.length; i++) {
75 var cookie = jQuery.trim(cookies[i]);
76 // Does this cookie string begin with the name we want?
77 if (cookie.substring(0, name.length + 1) == (name + '=')) {
78 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
85 var csrftoken = getCookie('csrftoken');
88 The functions below will create a header with csrftoken
91 function csrfSafeMethod(method) {
92 // these HTTP methods do not require CSRF protection
93 return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
95 function sameOrigin(url) {
96 // test that a given url is a same-origin URL
97 // url could be relative or scheme relative or absolute
98 var host = document.location.host; // host + port
99 var protocol = document.location.protocol;
100 var sr_origin = '//' + host;
101 var origin = protocol + sr_origin;
102 // Allow absolute or scheme relative URLs to same origin
103 return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
104 (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
105 // or any other URL that isn't scheme relative or absolute i.e relative.
106 !(/^(\/\/|http:|https:).*/.test(url));
110 beforeSend: function(xhr, settings) {
111 if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
112 // Send the token to same-origin, relative URLs only.
113 // Send the token only if the method warrants CSRF protection
114 // Using the CSRFToken value acquired earlier
115 xhr.setRequestHeader("X-CSRFToken", csrftoken);