Python AJAX not working PHP webpage - javascript

I am trying to make a website which when a certain button is pressed, a python script will run, which will turn on GPIO pins on my raspberry pi.
However my code doesn't seem to work. Please could someone tell me what is wrong with my code:
<html>
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#clickON').click(function(){
var a = new XMLHttpRequest():
a.open("GET","output.py")
a.onreadystatechange=function(){
if(a.readySate==4){
if(a.status == 200){
}
else alert("HTTP ERROR!!")
}PHP
}
a.send
});
});
</script>
</head>
<body>
<button type="button" id="clickON">ON</button><br>
</body>
</html>

Debugging Javascript can be done through your browser's developer tools. In Chrome for OSX, I access them by right clicking on any page element and clicking 'Inspect'.
I saved your code, opened the file and in the 'Console' tab I see:
Uncaught SyntaxError: Unexpected token :
This error is on line 8:
var a = new XMLHttpRequest():
Either remove this colon, or replace it with a semicolon, and you may be one step closer to fixing your problem.

Related

How do you allow html and Javascript files to function in Atom?

Code on webpageThe button doesn't workSo, I am fairly new to JavaScript and have been looking at tutorials for the language. I downloaded the Atom IDE and began following a video and used the same code as the video to make a web page with a button that reveals a message when clicked. However when I click the button nothing happens, and when I hit inspect element.
It gives me the following error: Failed to load resource: net::ERR_FILE_NOT_FOUND
It appears as though the HTML file is not working with the .js file. However they are in the same directory.
Here's the updated code (still the same error):
<DOCTYPE! html>
<html>
<head>
</head>
<body>
<button onclick="revealMessage()">Click me!</button>
<p id="hiddenMessage" style="display:none">Filosofem</p>
</body>
<script>
function revealMessage() {
document.getElementById("hiddenMessage").style.display = 'block';
}
</script>
</html>
Perhaps, it is the file directory. When you declared <script src="js/script.js"></script>, that means that your script.js file, must be located in a subfolder called "js".
If you want your to be within your file, then you can do
<DOCTYPE! html>
<html>
<head>
</head>
<body>
<button onclick="revealMessage()">Click me!</button>
<p id="hiddenMessage" style="display:none">Filosofem</p>
</body>
<script>
function revealMessage() {
document.getElementById("hiddenMessage").style.display = 'block';
}
</script>
</html>
Finally, you should try resaving the code through atom, and make sure your code is the right version of your code.
By using an external stylesheet you need to make sure your CSS file is saved as said above but with <script scr="filename.js">/script> I still haven't found a way other than having your code internal which is completely fine unless you're making a world wide website then it would be easy to hack.

<script> and JavaScript not working in HTML file for all browsers after XMLHTTPREQUEST

Hello I am new to JavaScript. I have looked through other posts but I cannot resolve my issue.
Earlier I was attempting to play around with the following script found here (How to read text file in JavaScript).
I got it working successfully SEVERAL times:
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>Read File (via AJAX)</title>
<script type="text/javascript">
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
function loadFile() {
reader.open('get', 'test.txt', true);
reader.onreadystatechange = displayContents;
reader.send(null);
}
function displayContents() {
if(reader.readyState==4) {
var el = document.getElementById('main');
el.innerHTML = reader.responseText;
}
}
</script>
</head>
<body>
<div id="container">
<input type="button" value="test.txt" onclick="loadFile()" />
<div id="main">
</div>
</div>
</body>
</html>
While testing this script, all of a sudden it stopped working! I have assured that my content blockers are disabled and javascript is enabled on my web browsers. I am not sure if I locked up my web browsers ability to use javascript while using this XMLHTTPREQUEST or possibly overloaded it. I even tried inserting a reader.abort() function to possibly close the request if it was still open somehow. I tried inserting alert() functions to troubleshoot that did not work. I tried restarting my computer that did not work. I am using a MAC and have tried the latest versions of Firefox, Chrome, and Safari which all do not work with javascript now.
It is so bad that I CAN'T even get this simple javascript example to work.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type=“text/javascript”>
document.write(“<h1>This is a test</h1>”);
</script>
</head>
<body>
</body>
</html>
PLEASE HELP! I don't know what I did or how to fix the issue.
Above script works only if you're loading the text file from a server. Make sure the file is located in the same place as your html file on the server and load the page with your server address. Most likely thing you're doing wrong is you're trying to access client's file. It doesn't work because that will be HUGE security breach, only the client can send you a file to read (If that is your intended purpose, then I advise you to use FileReader, example code can be found here).
About your second script, you're using “ instead of ".

simple jquery didnt work, and returning error

I have simple code to hide and show paragraph, and using jquery as the trigger
suddenly my jquery return errors,this is the error
SyntaxError: expected expression, got '<'
http://localhost/portofolio1/navigation_test.php/jquery-2.1.3.js
Line 1
ReferenceError: $ is not defined
http://localhost/portofolio1/navigation_test.php/
Line 9
if i use cdn it work perfectly as usual
this is my simple code to test my jquery
<!DOCTYPE html>
<html>
<head>
<title>Simple list</title>
<!-- <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>-->
<script src="jquery-2.1.3.js"></script>
<script>
$(function () {
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
<button onclick="">SHOW ALERT</button>
</body>
</html>
SOLVED
after i create new .php file and move ALL the code to the new file, it
work normal, i dont know the problem, this sure really weird
Mind the quotes:
<script src="./jquery-2.1.3.js"></script>
type is optional by the way.
Also check if http://localhost/portofolio1/navigation_test.php/jquery-2.1.3.js is pointing to jQuery. From the looks of your error, the script is pointing to something not jQuery, possibly an error page, hence SyntaxError: expected expression, got '<'. This error means that the browser expected a script, but returned HTML (signified by the script meeting an unexpected <).

jQuery ajax plugin get entered value

I am using jQuery autocomplete plugin to get values from database using this plugin. As per demo I made my code like this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" href="styles.css">
<script>
jQuery(document).ready(function() {
jQuery('#autocomplete').autocomplete({
serviceUrl: 'ajax.php',
});
});
</script>
</head>
<body>
<input type="text" id="autocomplete">
</body>
</html>
Now when I am doing any search. Then in console tab I can see the ajax request like this
XHR finished loading: GET "http://localhost/ajax.php?query=united
additionally it is showing one error in console tab like
Uncaught SyntaxError: Unexpected token < in jquery-2.1.0-min.js:4
Now to get the value of query string in ajax.php I did like this
$value = $_POST['query];
echo $value;
But it is not showing any value. So can someone tell me how to get the input entered values in the ajax.php file? Any help and suggestions will be really appreciable. Thanks
You are using Google Chrome, aren't you?
It would be helpful to see what data is actually transfered...
Try to hit F12, open the "Network" tab, click on the item that shows up when the AJAX request is made, and check the output in the "Response" sub tab.
My assumption: You're facing some syntax error in your PHP file that outputs HTML tags as part of an error message.
BTW: Your PHP code contains a syntax error, the ' after index "query" on line 1 is missing...

Javascript function not recognized in CRM 2011 HTML Web resource

Following up from my solved [previous issue][1], I'm having trouble building a simple HTML Web resource containing some basic javascript, page is rendered correctly but script doesn't seem to work properly.
My HTML resource is very basic:
<html>
<head>
<script src="ClientGlobalContext.js.aspx" />
<script type="text/javascript" src="new_jquery_1.7.2.min" />
<script type="text/javascript">
function buttonClick() { alert('Yo !'); }
</script>
</head>
<body>
<input type="button" value="Test" onclick="javascript: buttonClick();" />
</body>
</html>
Although the page shows up fine, clicking the button yields The value of the property is null or undefined not a function object error like the functions wasn't there, but I checked via F12 console that the code is rendered correctly.
I also tried invoking the web resource via the direct url, in the form of
http://mycrmserver/myorg/WebResources/new_myResource
But (as I expected) the behavior of the page was the same.
I checked Google, I surfed a couple of other SO questions and MSDN and all state this is the right way to do it, what's wrong with my code ?
Other (not sure if useful) details:
If the F12 tool is open the error comes up as a SCRIPT5007 javascript runtime error in the console. If it's not, I get the usual script error notify popup if I browse to the webresource direct url, or nothing happens at all if I try to open the resource inside the CRM.
The CRM environment is updated to Rollup 3 (updating it is not an option unfortunately)
I'm using IE 9 (Remember: Dynamics CRM can't be used in non-IE browsers yet)
UPDATE
Shorthand tags confuse the CRM.
Basically this syntax sometimes gets messed up:
<script src="ClientGlobalContext.js.aspx" />
But this works perfectly:
<script src="ClientGlobalContext.js.aspx"></script>
Root cause is a missing script tag, despite the code you posted being correct.
CRM does some messing about with the HTML you post into the script editor window. What is rendered in the browser is this (note that the ClientGlobalContext.js.aspx tag is not closed in the same way as your pasted code):
<HTML><HEAD>
<SCRIPT src="ClientGlobalContext.js.aspx">
<script type="text/javascript" src="new_jquery_1.7.2.min" />
<script type="text/javascript">
function buttonClick() { alert('Yo !'); }
</SCRIPT>
<META charset=utf-8></HEAD>
<META charset=utf-8></HEAD>
<BODY><INPUT onclick=javascript:buttonClick(); value=Test type=button></BODY></HTML>
Resolution:
Add full "close" tags to each opening script tag (rather than using "/>").

Categories

Resources