Uncaught Error: Error calling method on NPObject. - javascript

am a newbie to JSON and i just cant's access to the object data.
this is the code :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ajax </title>
</head>
<body>
<p id="p"></p>
<script type="text/javascript" >
var r = new XMLHttpRequest()
r.open("GET", "d.json");
if(r.readyState ===4 && r.status ===200){
var data = JSON.parse(r.responseText);
var p = document.getElementById("p");
p.innerHTML = data.user;
}
r.send();
</script>
</body>
</html>
and this is the d.json file
{"user":"khaled","age":"20"};
i got that error : "Uncaught Error: Error calling method on NPObject. "
thanks.

if its a flash url ur dealing with then this link will help
npe flash
else
a general discussion in google comm
npe google

Related

Simple web example showing unresolved "ReferenceError: $ is not defined" [duplicate]

This question already has answers here:
JQuery - $ is not defined
(36 answers)
Closed 5 months ago.
I am trying to call a php from a javascript.
My code is longer, but I'm trying this simple example to see if the call works.
The example consists of three files and when opening in the browser
localhost/myweb/mytest/example01/index.html
the alerts that appear in index.js should be displayed and it calls index.php, which should write a message to a log file.
The files I use in my test example are:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF'8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello world</title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
index.js
alert("Hello world from javascript!");
debugger;
var myip = "192.168.1.20";
var myfile = "My_file_name";
$.get( 'index.php',{ paramIp: myip, fileSource: myfile },
function( response ) {
if ( response.status == 'success' ) {
alert("PHP returns OK");
}
else {
alert("PHP returns ERROR");
}
}
)
index.php
<?php
$paramIp = (isset( $_GET['paramIp'] ))? $_GET['paramIp']: false;
$fileSource= ( isset($_GET['fileSource'] ))?$_GET['fileSource']: "";
$fp = fopen("log_messages.txt", "a");
$date = (new DateTime("NOW"))->format("y-m-d H:i:s");
fwrite($fp, $date."index.php paramIp = " . $paramIp . ", fileSource = ". $fileSource . PHP_EOL);
fclose($fp);
echo $fileSource;
?>
When I open the page in the browser with the URL
localhost/myweb/mytest/example01/index.html
The javascript alert is displayed, appearing the message
Hello world from javascript!
But when continuing the execution of the script, I see that the php does not get executed and in FireBug the error is shown:
ReferenceError: $ is not defined
After consulting some posts in several forums that talk about this error, I have modified the original files, leaving them like this:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="index.js"></script>
<meta charset="UTF'8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello world</title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
index.js
$(document).ready(function(){
alert('Hi from jQuery!');
console.log('Hi from jQuery!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
alert("Hello world from javascript!");
debugger;
var myip = "192.168.1.20";
var myfile = "My_file_name";
$.get( 'index.php',{ paramIp: myip, fileSource: myfile },
function( response ) {
if ( response.status == 'success' ) {
alert("PHP returns OK");
}
else {
alert("PHP returns ERROR");
}
}
)
index.php
<script type="text/javascript" src="index.js"></script>
<?php
$paramIp = (isset( $_GET['paramIp'] ))? $_GET['paramIp']: false;
$fileSource= ( isset($_GET['fileSource'] ))?$_GET['fileSource']: "";
$fp = fopen("log_messages.txt", "a");
$date = (new DateTime("NOW"))->format("y-m-d H:i:s");
fwrite($fp, $date."index.php paramIp = " . $paramIp . ", fileSource = ". $fileSource . PHP_EOL);
fclose($fp);
echo $fileSource;
?>
As before, the javascript alert is displayed, with the message
Hello world from javascript!
But the php does not get executed and in FireBug the error is still shown:
ReferenceError: $ is not defined
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
alert("Hello world from javascript!");
debugger;
var myip = "192.168.1.20";
var myfile = "My_file_name";
$.get( 'index.php',{ paramIp: myip, fileSource: myfile },
function( response ) {
if ( response.status == 'success' ) {
alert("PHP returns OK");
}
else {
alert("PHP returns ERROR");
}
}
)
});
</script>
And if you do that....?

ws.message is undefined Error, Web Socket response

I am python developer but i need to see my websocket output from browser.
function startSocket() {
var ws = new WebSocket("ws://localhost:8765/")
ws.onopen = function(event) {
ws.send("Sent this from client.js")
console.log(typeof ws.message);
console.log("test")
console.log(ws.message)
}
}
startSocket();
this is my clien.js file.
<!DOCTYPE html><html lang="en">
<head>
<!-- <link rel="stylesheet" type="text/css" href="style.css">-->
<link type="text/css" href="styles.css">
<meta charset="utf-8">
</head>
<body>
<script src="client.js"></script>
</body></html>
and this is my html file.
I am sending a string from my web socket and i want to see inside of a browser.
When i run this html, my web socket is starting to work but my logs are saying undefined.
This is my Log Output
client.js:6 undefined
client.js:7 test
client.js:8 undefined
read more here
bind to the correct event handlers
function startSocket() {
var ws = new WebSocket("ws://localhost:8765/");
ws.onopen = function(event) {
console.log("connection opened");
ws.send("Sent this from client.js")
};
ws.onmessage = function(message) {
console.log(typeof message);
console.log("test")
console.log(message)
};
};
startSocket();

Unexpected token : Javascript jsp

I am novice in the JSP application, where we have to pass a url variable from servlet to JSP page using request.getattribute. While passing the data to the javascript function we are getting error below is
Uncaught SyntaxError: Unexpected token :
Please find below the JSP page.
<head>
<%String urlData=(String)request.getAttribute("urlDataFromServlet");%>
<script type="text/javascript">
var url=<%=urlData%>;
$(document).ready(function() {
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
Any help will be appreciated.
Thanks in advance.
You need to wrap this with quotes:
var url="<%=urlData%>";

What is wrong with this HTML, JavaScript and JSON code?

I have the following HTML code in a file called test.html. Both the HTML file and the JSON file below are stored on a server within the same directory.
<!DOCTYPE html>
<html>
<head>
<title>Shape Up</title>
<meta name="robots" content="noindex,follow">
<meta name="description" content="">
<meta name="keywords" content="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script language="JavaScript" type="text/javascript">
function ajax_get_json()
{
var hr = new XMLHttpRequest();
hr.open("GET", "game.json", true);
hr.setRequestHeader("Content-type", "application/json", true);
hr.onreadystatechange = function()
{
if(hr.readyState == 4 && hr.status == 200)
{
var data = JSON.parse(hr.responseText);
var results = document.getElementById("results");
results.innerHTML = "";
for(var obj in data)
{
results.innerHTML += data[obj].title;
}
}
}
hr.send(null);
results.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="results"></div>
<script language="JavaScript" type="text/javascript">
ajax_get_json();
</script>
</body>
</html>
It pulls data from a file called game.json which is stored in the same directory.
{
"level_001":
{
"id":001,
"title":"Level 1",
"difficulty":0,
"comments":"this is how you complete level 1"
},
"level_002":
{
"id":002,
"title":"Level 2",
"difficulty":0,
"comments":"this is how you complete level 2"
}
}
The problem is that the results.innerHTML = ""; line is never reached. Why?
There are no errors in the browser, I've checked this on Firefox and on Safari.
According to jsonlint.com your JSON is invalid because of these properties:
"id":001
...
"id":002
You need to either remove the leading zeros:
"id":1
or make the numbers strings:
"id":"001"
For further details see the format rules spelled out at json.org
Presumably the line you mentioned is never reached because JSON.parse() gives an error about the above. (Do you not see an error in the browser's console?)

How to download external file using phonegap?

I am trying to download an apk file on a button click using phonegap. Why does this code not work? Nothing happens when I click Download. Could someone point me in the right direction? Thanks.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<script charset="utf-8" src = "jquery-1.10.1.min.js"></script>
<script charset="utf-8" src = "cordova-2.7.0.js"></script>
<script>
function foo()
{
var fileTransfer = new FileTransfer();
fileTransfer.download(
"http://samplewebsite.com/example.apk",
"file:///sdcard/example.apk",
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
}
);
}
</script>
</head>
<body>
<button onclick="foo()">Download</button>
</body>
</html>
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
var filePath= "/sdcard/directory/file.extension";
fileTransfer.download(uri,filePath,
function(entry) {
//success
},
function(error) {
//failed
}
);
This worked for me
Your code works fine.
This seems stupid but have you tried to use alert() instead of console.log() in the callbacks?
If you are sure the callback code is not invoked try to run just the app created by the phonegap' create script and check that the device is ready before doing other tests.
just my 2c

Categories

Resources