I've tried to load external html page on same server using jQuery .load() by clicking on div , but it just doesn't work ! I tried using examples of $.get or $.post from Jquery API website . but every time I found this error in my chrome console :
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
every correct answers on Stackoverflow also had same results .
I know a little of JavaScript,Jquery and php . can somebody help me out here ?
Extra Notes : I've also tested my codes in XAMP local server ;
I've tried many ways , the last time my code was :
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('#reslink').click(function(e){
e.preventDefault();
$.ajax({
type: "GET",
url: "2.html",
data: { },
success: function(data){
$('#maincont').html(data);
}
});
});
});
</script>
</head>
<body>
link1
<div id="maincont">
</div>
</body>
</html>
new:
I tested the same code in online free webhost and it just worked there ! I still cand understand the reason why it didnt work in xamp server !
Related
I have a python program on remote server. I need to create a web page (html code present in same directory as that of python script on server) having a button on clicking which python script should run. One more thing is that we need to choose a file from local machine after which the python script takes that file as input, runs and outputs another file which needs to be displayed on web page.
I don't know what to use javascript or ajax or php to achieve this. I tried out different ways but in vain.
This is the html code I have been trying with...
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
// var file = $('#fileInput')[0].files[0]
var fd = new FormData();
fd.append('fileInput', file);
// console.log(fd)
$.ajax(
{
type: "POST",
url: "./summarize.py",
data: "fd"
success: function (response) {
}
// error: function (response) {}
});
});
});
</script>
</head>
<body>
<h3>Upload reviews file</h3>
<input type="file" id="fileInput" name="fileInput">
<p>Click the "Get summary" button to summarize the reviews.</p>
<button>Get summary</button>
</body>
</html>
I have searched online but no where the answer was specific (I felt so). Since I am new to javascript, I have trouble in following them. Someone kindly explain what is to be done.
Thank you.
I think you can use php exec, to run Python and your summarize.py script.
<?php
// #result array
exec("python summarize.py", $result);
PHP exec will return you result. Also you can use django or other frameworks to run your Python code.
My aim is to get an element <div id="calender"> and all what is in the element shown in a browser. The point is that normal get-html-source won't do the thing. The element what I am looking for does not exists in the html output of php-function file_get_contents.
I have tried to get the source by php with xpath byt the help of http://us3.php.net/manual/en/class.domxpath.php which inludes a nice tool to get what is in any tag in the html page. But the problem here might be that the element (a calender) is formed to the loaded page by javascript and cannot be caught by server side php. So, is there a way I can catch such element (div) by javascript instead.
There are script examples of javascript for this kind of problem (if I have understood them correctly) but currently I cannot get a simple javascript to work. An example below shows how I have tried to built up a code. $ajax thing here is just one path I have tried to solve the problem but don't know how to use it. More here I cannot figure out why the simple javascript functions do not work (just test purposes).
<!doctype html>
<html lang="fi">
<head>
<meta charset="utf-8">
<title>load demo</title>
<style>
body {
font-size: 12px;
font-family: Arial;
}
</style>
<script type="text/javascript">
function ok {
alert "OK";
}
function get_html (my_html){
alert "OK";
var l = document.getElementById('my_link').value;
alert l;
alert my_html;
var url = my_html;
$.ajax({
url: url,
dataType: 'html'
success: function(data){
//do something with data, which is the page 1.html
var f = fs.open("testi_kalenteri.html", "w");
f.write(data);
f.close();
alert "data saved";
}
});
}
</script>
</head>
<body>
<p id ='my_link' onclick='get_html("lomarengas.fi/en/cottages/kuusamo-rukasaukko-9192")'>html-link</p>
<p id ='ok' onclick='ok()'>show ok</p>
</body>
</html>
Briefly, I have a link to a web page, which shows up a (booking) calendar in it but this calendar is missing in the "normal" source code, by file_get_contents (php). If I browse the html source with Chromes tools (F12) I can find the calendar there. T want that information get by javascript or by php or such.
If you read the source code of the page you point to (http://www.yllaksenonkalot.fi/booking/varaukset_akas.php), you notice that the calendar is loaded via an iframe.
And that iframe points to that location :
http://www.nettimokki.com/bookingCalendar.php?id_cottage=3629&utm_source=widget&utm_medium=widget&utm_campaign=widget
Which is in fact the real source of the calendar...
EDIT following your comment on this answer
Considering the real link : http://www.lomarengas.fi/en/cottages/kuusamo-rukasaukko-9192
If the calendar is not part of the generated html, it is surely asynchronously generated (in javascript, client side).
From this asumption, I inspected the source code (again).
In the developper tools of my browser, in the Network section, where you can monitor what files are loaded, I looked for
calls to server (everything but calls to resources : images, stylesheets...).
I then noticed calls to several urls with json file extensions like http://www.lomarengas.fi/api-ib/search/availability_data.json?serviceNumber=9192¤tMonthFirstDate=&duration=7.
I felt I was on the right track (asynchronous javscript calls to generate html with json datas), I looked for javascript code or files that was not the usual libraries files (jquery, bootstrap and such).
I stumbled upon that file : http://www.lomarengas.fi/resources_responsive/js/destination.js.
It contains the code that generates asynchronously the calendar.
tl;dr
The calendar is indeed generated asynchronously.
You can't get the full html with a curl or file_get_content in PHP and
you can't access it with ajax code (due to Same-origin policy).
By the way, you should contact the site to see if you can access their api via PHP with their consent.
Hope it helped you understand the whole thing...
To get <div id="calender"> you can use next code (jquery):
<div id="calender"></div>
<script>
$("#calendar").click(function(){
alert('calendar was clicked');
});
</script>
If I understand you correctly. I think you need appropriate php respond with some correct code inside php file:
// json_handler.php
<?php
if (is_ajax()) {
$return = $_POST;
$return["ok"]="ok";
$return["json"] = json_encode($return);
echo json_encode($return);
}
function is_ajax()
{
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
and this is script wich is inside html:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<a id="click">click</a>
<script>
$("document").ready(function(){
$("#click").click(function(){
var data = {
"request": "request"
};
data=$.param(data);
// alert(data);
$.ajax({
type: "POST",
dataType: "json",
url: "json_handler.php",
data: data,
success: function(data) {
// here you will see echo respond from your php json_handler.php
// also you can add here more javascript (jquery code) to change your page after respond
alert();
}
});
return false;
});
});
</script>
<body>
<html>
http://www.w3schools.com/jquery/jquery_ajax_intro.asp
Trying to create a FAN area on facebook where I need to post all products available only to my facebook fans.
My php file creates plain HTML, which I would like to grab from Facebook end.
How this can be done if this can be done at all?
The following is entire content of my script.js file
$.ajax({
type:"POST",
url:"https://www.mysite.com/includes/facebook_fan_page.php",
data: data,
dataType:"html",
error:function(){},
success:function(response){
$('#products').html(response);
}
})
and here is my index.html
<html>
<head>
[[style.css]]
<script type="text/javascript" src="https://www.mysite.com/js/jquery-
1.8.2.min.js"></script>
[[script.js]]
</head>
<body>
<div class="content">
<div id="products"></div>
</div>
</body>
</html>
I do not know how to write output in to the empty div that I have on my apps index.html page <div id="products"></div>
Also, the app I use on facebook is Static HTML iFrame Tabs, if it makes any difference
Please help
Thanks to you all in advance
My skills of ajax and js are very limited, so please be patient with me on this one.
This is also a method for Ajax Post request, I find it simple. Tell me if you find this useful
In HTML Page
In script file or tag
$.POST("filename.php", data, function(response) {
if(!response) {
alert("Oh Snap! I hate this part");
}
else {
$("#div").html(response.field);
}
});
Read about his here AJAX POST REQUEST
I was wondering what a good way to load an external web page (same server) would be. I have tried .load() and .get() however, The external page has a php script that spits out information every few seconds, the .load() and .get() only load it after the php is done. I have tried iFrame with does load it displaying the information being outputted by the PHP script. However, I don't really like to use iFrames. Thanks!
If your goal is for the PHP information (that is spit out every few seconds) to be updated on your site, then what you want to do is use AJAX, inside a setInterval routine.
See this post for the basics of AJAX -- it really is simpler than you might think. (You might first want to look at the simple examples linked at bottom).
Once you've got a simple ajax exchange happening, put that into a function called, for example, doAjax() -- and then create a setInterval, like this:
setInterval('doAjax();',60000);
Here is an important note when considering setInterval
Following is a simple copy/paste(able) example that will let you see exactly what I mean:
HTML/javascript: index.php
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style>
#timeDiv{width:40%;height:200px;background:wheat;padding:10px;}
</style>
<script type="text/javascript">
$(document).ready(function() {
doAjax();
window.setInterval(function(){
doAjax();
},2000);
}); //END document.ready
function doAjax() {
$.ajax({
type: "POST",
url: "your_php_processor.php",
success: function(myData) {
$('#thetime').html(myData);
}
});
}
</script>
</head>
<body>
<div id="timeDiv">
The time is: <span id="thetime"></span>
</div>
</body>
</html>
Now, the PHP side... your_php_processor.php
<?php
$d = date("h:i:s");
echo $d;
The Ajax below is on a file with a Facebook Like button. The Like button works correctly. I am trying to use the Ajax below to execute some PHP on a file called fblike.php when the Facebook Like button is clicked. It's not working. Is there any way I can check to see if FB.Event.subscribe('edge.create', function(response) {}); is actually firing?
Thanks in advance,
John
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script >
FB.Event.subscribe('edge.create', function(response) {
$.ajax({
url: "fblike.php", // the url of your php script
context: document.body,
success: function(){
// if you want something to be executed when a result comes back
}
});
});
</script>
</head>
<body>
?>
Yes, use firebug or the inspection tool in chrome/safari, or even IE Developer toolbar. Navigate to the NET tab, and see if any requests are sent to 'fblike.php', alernatively add console.log or alert() messages to both success, and error callbacks.