X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=portal%2Fstatic%2Funbound_reservation_static%2Fjs%2Fcreatepost_.js;fp=portal%2Fstatic%2Funbound_reservation_static%2Fjs%2Fcreatepost_.js;h=b19b5cd7b1456c465d3c13cfd336482cf2f282ca;hb=729a9dbb380b51a217194ba2a4e5978186fe50b0;hp=0000000000000000000000000000000000000000;hpb=c4bd5da6e2630eddf1262aa8d808dbb48b097d53;p=unfold.git diff --git a/portal/static/unbound_reservation_static/js/createpost_.js b/portal/static/unbound_reservation_static/js/createpost_.js new file mode 100644 index 00000000..b19b5cd7 --- /dev/null +++ b/portal/static/unbound_reservation_static/js/createpost_.js @@ -0,0 +1,120 @@ +$(function() { + + + // Submit post on submit + // $('#b1').bind('click', function(event){ + // event.preventDefault(); + // console.log("form submitted!") // sanity check + // create_post(); + // }); + +$('#b1').bind('click', function(event){ + event.preventDefault(); + console.log("form submitted!") // sanity check + jsondata=document.getElementById("jsondata").value; + create_post(jsondata); + }); + // AJAX for posting + function create_post(jsondata) { + $("button").hide(300); + $("#but").show(6000); + $('#but').html(' loading...'); + console.log("create post is working!") +console.log(JSON.stringify(jsondata)); +var obj = jQuery.parseJSON(jsondata); +console.log(JSON.stringify(obj)); + // sanity check + var myEvent = {"resources":[{"type": "Node"},{"type": "Node"}]}; + console.log(JSON.stringify(myEvent)); + //jsondata=JSON.parse(JSON.parse(json).jsondata); + jsondata=jsondata.replace("\\", ""); + console.log(jsondata); + $.ajax({ + url : "create_post/", // the endpoint + type : "POST", // http method + data : JSON.stringify(obj), // data sent with the post request + // handle a successful response + complete:function(){ + //alert("Request completed"); + $("#but").hide(); + $("button").show(); + }, + success : function(data,status) { + + console.log(data,status); // log the returned json to the console + + var obj = JSON.parse(data); + // alert(data); + for (var i = 0; i < obj.resource_response.resources.length; i++) { + var k=i+1; + var m='#'+k; + input_2 = " Node-"+k+""+ + //"
"
+obj.resource_response.resources[0].domain+"
"+obj.resource_response.resources[0].name+"
"+ + "
domain:"+obj.resource_response.resources[i].domain+"
name:"+obj.resource_response.resources[i].name+"
"+obj.resource_response.resources[i].urn+"
"; + $(m).html(input_2); + } + + console.log("success"); // another sanity check + }, + // handle a non-successful response + error : function(xhr,errmsg,err) { + $('#results').html("
Oops! We have encountered an error: "+errmsg+ + " ×
"); // add the error to the dom + console.log("papapa" + xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console + } + }); + }; + + + // This function gets cookie with a given name + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + var csrftoken = getCookie('csrftoken'); + + /* + The functions below will create a header with csrftoken + */ + + function csrfSafeMethod(method) { + // these HTTP methods do not require CSRF protection + return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); + } + function sameOrigin(url) { + // test that a given url is a same-origin URL + // url could be relative or scheme relative or absolute + var host = document.location.host; // host + port + var protocol = document.location.protocol; + var sr_origin = '//' + host; + var origin = protocol + sr_origin; + // Allow absolute or scheme relative URLs to same origin + return (url == origin || url.slice(0, origin.length + 1) == origin + '/') || + (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') || + // or any other URL that isn't scheme relative or absolute i.e relative. + !(/^(\/\/|http:|https:).*/.test(url)); + } + + $.ajaxSetup({ + beforeSend: function(xhr, settings) { + if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) { + // Send the token to same-origin, relative URLs only. + // Send the token only if the method warrants CSRF protection + // Using the CSRFToken value acquired earlier + xhr.setRequestHeader("X-CSRFToken", csrftoken); + } + } + }); + +}); \ No newline at end of file