reservation plugin - unbound request (unclean
[unfold.git] / portal / static / unbound_reservation_static / js / createpost_.js
1 $(function() {
2
3
4     // Submit post on submit
5    // $('#b1').bind('click', function(event){
6     //    event.preventDefault();
7    //     console.log("form submitted!")  // sanity check
8    //     create_post();
9   //  });
10  
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);
16     });
17     // AJAX for posting
18     function create_post(jsondata) {
19         $("button").hide(300);
20           $("#but").show(6000);
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));
26         // sanity check
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);
32         $.ajax({
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
37                         complete:function(){
38                         //alert("Request completed");
39             $("#but").hide();
40                         $("button").show();
41         },
42             success : function(data,status) {
43                 
44                 console.log(data,status); // log the returned json to the console
45                
46                            var obj = JSON.parse(data);
47                           // alert(data);
48                         for (var i = 0; i < obj.resource_response.resources.length; i++) {
49                         var k=i+1;
50                         var m='#'+k;
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>";
54                            $(m).html(input_2);
55                            }
56                            
57                 console.log("success"); // another sanity check
58             },
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'>&times;</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
64             }
65         });
66     };
67
68
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));
79                     break;
80                 }
81             }
82         }
83         return cookieValue;
84     }
85     var csrftoken = getCookie('csrftoken');
86
87     /*
88     The functions below will create a header with csrftoken
89     */
90
91     function csrfSafeMethod(method) {
92         // these HTTP methods do not require CSRF protection
93         return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
94     }
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));
107     }
108
109     $.ajaxSetup({
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);
116             }
117         }
118     });
119
120 });