get html code from a div - html

Im getting the html code from div with html() like this :
var s = $("#content").html();
and sending them to create a new page with this content with ajax :
$.ajax({
method: "POST",
url: "create.jsp",
data: {PageContent: s,
AppName: name,
header : temp
},
dataType: "html"
});
and this is how I create it with java :
File strFile = new File(strPath);
Writer objWriter = new BufferedWriter(new FileWriter(strFile));
objWriter.write(header);
objWriter.write(content);
objWriter.write(" </div>");
objWriter.write(" </body>");
objWriter.write("</html>");
objWriter.flush();
objWriter.close();
Everything is good except when I open the new file I found all the html code in one line ! anyone knows a solution?
Update
it seems that the html() method is extracting the code in one line as i tested ! so is there any solution?

Related

Sending variable and image via ajax

Could anybody please help me with this as i have tried several different methods all to no avail.
im trying to send the old image src name along with the new image data via ajax, but i can only manage to send 1 or the other and not both..
file = input.files[0];
newimagesrc=input.files[0].name;
oldimagesrc=oldsrc;
formData= new FormData();
formData.append("image", file , newimagesrc);
recent attempt failed formData2= new FormData();
recent attempt failed formdata2.append('oldimage', oldimagesrc);
$.ajax({
url: "UploadProfileImage.php",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(messagereturn1){
alert(messagereturn1);
}
});
No buttons or forms im afraid, strictly passing variable's onchange / onclick.
Not sure how to send the old image src through ajax as well as the new image data , i would like to send more variables if possible that have been passed into JS function..
var form_data = new FormData(); // Creating object of FormData class
form_data.append("image", file , newimagesrc) // Appending image parameter
form_data.append("oldimagesrc", oldimagesrc) //added a second variable then received in PHP as a $_POST['oldimagesrc']
PHP
if (isset($_POST['oldimagesrc'])){ $oldimagesrc=$_POST['oldimagesrc']; }

Transmitting a JSON-Object to a new HTML side

Hello guys i have a big problem, which i somehow cant fix. I am having some JSON- Objects which i am giving out with a table, this part works perfectly fine. But than i would like to have a button or link that posts one of this JSON- Objects (the ID) to a new .html page. In this new .html page i would like to use the ID in Javascript code.
Does anybody know if there is a way how i can accomplish this and if yes how can i do it?
I tried to post it via the php _get methode but it just doesnt work it tells me that there is an " Uncaught TypeError: Cannot read property 'value' of null"
My JS-Code looks like this:
function dataRequest() {
var output = $('#output').text('Loading data...');
$.ajax({
url: 'http://test/html/getmeineFahrten.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
output.empty();
var ort ='<table border="dashed" width="30%">'
$.each(data, function(i,item){
var ServerDatum = item.abfahrt
var Datumonly = ServerDatum.substring(0,4) + ServerDatum.substring(5,7) + ServerDatum.substring(8,10);
var calcDatum = Datum - Datumonly
if(item.user_user_id == userID){
if(calcDatum < 0){
ort += '<tr>'+'<td>'+item.abfahrt+'</td>'
+ '<td>'+item.rueckfahrt+'</td>'
+ '<td>'+item.StartOrt+'</td>'
+ '<td>'+item.Zielort+'</td>'+'<td>'+'<form action="http://test/html/meineFahrten_Karte.html?FahrtenID= + item.fahrten_id" method="get"><input type="submit" value="Karte">'+'</form>'+'</td>'+'</tr>';
}}
});
ort +='</table>';
output.append(ort);
},
error: function(){
output.text('There was an error loading the data.');
navigator.notification.confirm(
'Something went wrong. Would you like to retry?',
yourCallback,
'Error',
'No,Yes'
);
}
});
}
You can't access URL parameters unless you create your own function to do so. Unfortunately, there is no native function for this yet, however, user Quentin answers this question in greater detail and provides a function for getting URL parameters.
How to get the value from the GET parameters?

How to feed an image slider from a flat JSON file by Handlebars template?

I want to feed an image slider (flexslider) from a flat JSON file by Handlebars template. for that I tried this.
$.ajax({
type : 'GET',
dataType : 'json',
async: false,
url: window.location.href + "JSON/carousel-data.json",
success : function(data) {
console.log(data);
var src = document.getElementById("sliderTemplate").innerHTML;
var tmpl = Handlebars.compile(src);
$('#imageslider').innerHTML = tmpl(data);
$('.flexslider').flexslider({animation: "slide"});
}
});
But slider is not instantiated by this code.
However if I assign the same content of JSON file to a variable in same JS file and pass it to Handlebars template it works fine.
What is the problem with my ajax code?
I found the solution, everything is fine but one line that is
$('#imageslider').innerHTML = tmpl(data);
It should be
$('#imageslider').HTML(tmpl(data));
As it follows the jQuery syntex.

jQuery and JSON vs IE - SCRIPT5007: Unable to get value of the property

I'm struggling to get this script working. It's basically a simple ajax call to retrieve data from a php which returns a JSON code.
function refreshWindows(){
if(AjaxPull && AjaxPull.readystate != 4){
AjaxPull.abort();
}
AjaxPull = $.ajax({
type: 'POST',
url: $path,
data: {
ajax: true,
mode: 'update',
to: Math.round(currentDate.getTime() / 1000),
from: Math.round(previousDate.getTime() / 1000)
},
dataType: "json",
success: function (data) {
alert(data); //that's for debug
$replies = data.Updates;
$.each($replies ,function(group,value) {
if (value!=''){
$("#group"+group+" .content").append(value);
$("#group"+group+" .content").stop().animate({ scrollTop: $("#group"+group+" .content")[0].scrollHeight }, 800);
if (!$("#group"+group+" .Window").is(':visible')) {
$("#group"+group+" .BottomBox").fadeTo('fast', 0.5).fadeTo('fast', 1.0);
}
}
});
previousDate = currentDate;
currentDate = new Date();
timeController.push( setTimeout(function(){refreshChatWindows();}, 500) );
}
});
}
The error I get in Internet Explorer is:
SCRIPT5007: Unable to get value of the property 'Updates': object is null or undefined
Everything works fine in Firefox and Google Chrome.
Initially my code was made using .get but someone suggested switching to the .ajax - well, it didn't help. I tried using .done(function(data){ but it didn't work either. I also tried sending all of the data in my URL opposite to the data property, it worked fine in FF, but IE still popped the same error. Finally I tried adding different headers to the PHP, like for example header('Content-Type: application/json'); but it didn't change anything. I run out of ideas / possible solutions I could fine on stackoverflow, so any help would be appreciated.
In IE I went to Developer Tools, network tab and tried to see if everything works - yes, the request is being sent correctly with all the data, and a response I receive is correct JSON, just as it is in Firefox:
{"Updates":{"1":"","2":"","3":"","5":"","6":"","7":"","8":""},"time":{"from":"1367489761","to":"1367489761"}}
which gets me really confused, cause I'd have thought that Undefined error might happen only because something is not being sent back in IE for whatever reason, but clearly: It's not the case. I get my JSON back. Only IE for some unknown reason still thinks that data is undefined.
Ok, I found a solution finally.
Basically:
Remove any headers sent by PHP script. Especially: Content-type headers. (luckily - seems like sessions still can be used)
Use }).done(function ( data ) { instead of success: function (data) {
and that's all. Suddenly it started to work. It's very weird. Seems like the shotgun tactic (randomly changing bits of code till it works) is actually a valid way of solving problems. hehehe
I have a similar json call that returns data that looks like this:
{"GetTombstoneDataRestResult":{"AlphaSort":"Arai Junichi","Classification":"A&D Design Object"...etc
In other words, a lot like your json data. To reference it in jQuery, I use the callback name.
$.ajax({
type: "GET",
dataType: "jsonp",
url: url,
success: function (result) {
$('#screenshot').append('<p><strong>Title: ' +
result.GetTombstoneDataRestResult.Title + '</strong><br />Year: ' +
result.GetTombstoneDataRestResult.Dated + '<br />Artist: ' +
result.GetTombstoneDataRestResult.DisplayName + '</p>');
}
});
Looks like you want to try this too:
var replies = data;
$.each(replies.Updates ,function(group,value) {
You have a character called ​ in your JSON.
See a description here:
What's HTML character code 8203?
It's right before your colon here "time"​:
Can you clean your output and validate the JSON and try again?

How do I convert an html table to an Excel File?

I'm trying to get my page Data.aspx to return an html table, but I want that table to be in an excel format. This is my code so far.
In the Data.aspx Load:
Response.Clear()
Select Case lstrCmd
Case "summary"
Response.Write(Summary())
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Case "export"
Response.ContentType = "application/ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=" & _
mstrProjectName & ".xls")
Response.Write(SummaryExport())
End Select
Response.End()
So the add header is where I'm puzzled. This is something another peice of my software uses, but I'm not sure if it will work..
the jquery that calls this is using ajax:
function btnSubmitExport_Click() {
var compID = $("#ddlCompanies").val();
var projectID = $("#ddlProjects").val();
var startDate = $("#txtStartDate").val();
var endDate = $("#txtEndDate").val();
var compName = $("#ddlCompanies :selected").text();
var projectName = $("#ddlProjects :selected").text();
$("#content").html("<div class='loading'>loading...</div>");
$.ajax({
url: "Data.aspx",
data: {
CompanyID: compID,
ProjectID: projectID,
CompanyName: compName,
ProjectName: projectName,
StartDate: startDate,
EndDate: endDate,
Cmd: "export"
},
success: function(html) {
$("#content").html(html);
},
error: function(response) {
alert(response);
}
});
}
So, typically, when I request this data, I just throw the resulting html into a "content" div on my default.aspx page.. but I want that html to be an excel file!
Any hints or thoughts on this would be helpful.
I see the same question in other places, but not any answers that help me with my existing code..
If you have an html text contains Table tag in it, simple save it with extension of XLS and Microsoft Excel will open it like a charm! And also as I've seen, you want user to download the file, not to show its content in the page. You should redirect page on your ASPX page directly (without using JQuery). I'd suggest you to use network monitor in the developer tools to see what's happening.
Cheers
Normaly you would need to use this header
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "filename=excelfile.xls"
Print all the output as you want it in this specific page and that would be it.
Research on these headers and you will be fine.