Show some error Uncaught ReferenceError: - javascript

HI i have call a json file and show some error can u please help me
show error Uncaught ReferenceError: marketlivedata is not defined
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:g="http://base.google.com/ns/1.0">
<head>
<title>Data Call to json</title>
<script type="text/javascript">
// =====================
$(document).ready(function(e) {
// alert('hello');
// var marketlivedata ;
});
// =====================
function getUserData() {
$.ajax({
type: "GET",
url: "http://xxxxxxxxx.xxxxxxx.com/xxxxxxx.json",
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
// $('#ajexLoaderSec').hide();
console.log(data);
},
error: function(e) {
alert("There is an error while connecting to the server. Please try after some time");
}
});
};
getUserData();
</script>
</head>
<body>
gsdf sdf sdfsd sdf sd
</body>
</html>
My json format is
marketlivedata([{"sensex":{"trend":"equal","CloseIndexValue":"24893.81","premarket":"false","DateTime":"11:41 AM | 08 Sep 2015","CurrentIndexValue":"24958.31","Segment":"BSE","OpenIndexValue":"24972.01","PercentChange":"0.26","IndexName":"SENSEX","NetChange":"64.50"},"nifty":{"trend":"equal","CloseIndexValue":"7558.80","premarket":"false","DateTime":"11:41 AM | 08 Sep 2015","CurrentIndexValue":"7582.85","Segment":"NSE","OpenIndexValue":"7587.70","PercentChange":"0.32","IndexName":"CNX NIFTY","NetChange":"24.05"},"gold":{"ClosePrice":"26500.00","trend":"negative","OpenPrice":"26499.00","ExpiryDate":"2015-10-05","SpotSymbol":"SGOLDAHM","LastTradedPrice":"26441.00","DateTime":"8-September-2015 11:34:22","Symbol":"GOLD","PercentChange":"-0.22","CommodityName":"Gold","NetChange":"-59.00","PriceQuotationUnit":"10 GRMS ","SpotPrice":"26401.0"},"silver":{"ClosePrice":"35193.00","trend":"equal","OpenPrice":"35176.00","ExpiryDate":"2015-12-04","SpotSymbol":"SSILVERAHM","LastTradedPrice":"35070.00","DateTime":"8-September-2015 11:34:0","Symbol":"SILVER","PercentChange":"-0.35","CommodityName":"Silver","NetChange":"-123.00","PriceQuotationUnit":"1 KGS ","SpotPrice":"34815.0"},"USD/INR":{"DateTime":"2015-09-08 11:36:02.0","percentChange":"-0.27","netChange":"-0.18","name":"USD/INR","bidprice":"66.65"},"DXY Index":{"DateTime":"2015-09-08 11:39:40.0","percentChange":"-0.1","netChange":"-0.1","name":"DXY Index","bidprice":"96.13"},"marketstatus":{"currentMarketStatus":"Live"}}])

The returned data is marketlivedata(...). This is calling the marketlivedata function, which is not defined in your script. Since, you've used dataType as jsonp, the function is executed.
To solve this you can change the data format from the JSON server(which might not be possible as this looks like third party service) or you can define a function of that name which will be called when the response has arrived.
function getUserData() {
$.ajax({
type: "GET",
url: "http://mobilelivefeeds.indiatimes.com/homepagedatanew.json",
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
// $('#ajexLoaderSec').hide();
console.log(data);
},
error: function(e) {
console.log(e);
}
});
};
getUserData();
function marketlivedata(data) {
console.log(data);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

As you are using json data which comes from another domain so there would be a callback function needed to access this:
Uncaught ReferenceError: marketlivedata is not defined
Here marketlivedata is a Callback Wrapper function which is returned from the service you are hitting, so Either a reference of a global function has to be set with the name of marketlivedata or make use of jsonpCallback: 'callback'(much better one) where "callback" is the function from the response. This function is actually carrying the data which you want to use, So this has to be done:
function getUserData() {
$.ajax({
type: "GET",
url: "http://mobilelivefeeds.indiatimes.com/homepagedatanew.json",
dataType: 'jsonp',
crossDomain: true,
jsonpCallback: 'marketlivedata', // call the returned function here.
success: function(data) {
document.body.innerHTML = '<pre>' + JSON.stringify(data) + '</pre>'; // use the data as you need to.
}, // you can refere it here
error: function(e) {
console.log(e);
}
});
};
getUserData();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Related

how to detect a server error into a jquery ajax call?

I use Jquery to parse an json from url: the code is like this:
function fetchdata() {
var statusUrl = '/api/desk/j/';
$.ajax({
url: statusUrl,
dataType: "json",
type: 'post',
success: function(response) {
alert('ok');
},
error: function(xhr, status, error) {
var err = JSON.parse(xhr.responseText);
alert(err.message);
}
});
}
everything works fine, but if the server is not reachable I'm not able to detect it: I tried to do something in error: function, but seems that the code in error is fired only if the json has an error
have you got some ideas?
thank you!
You need to test the statusText from the jQuery textStatus response object. You can take advantage of your browser's developer console to inspect this object. You can expand the properties and methods for your perusal, however you wanna use it. Just click on the returned message of the console.log() to see these properties and methods that you wan't to use for error detection.
function fetchdata() {
var statusUrl = '/api/desk/j/';
$.ajax({
url: statusUrl,
dataType: "json",
type: 'post',
success: function(response) {
alert('ok');
},
error: function(textStatus) {
console.log(textStatus);
console.log(textStatus.statusText, textStatus.status);
}
});
}
fetchdata();

jQuery JSONP callback function not found

I'm trying to get data from external server using JSONP, but I've stuck on the callback function.
function processLinks(data) {
alert('IT WORKS!');
};
function getLinks() {
$.ajax({
type: 'GET',
url: 'https://external-site.com/test.cgi',
dataType: 'jsonp',
success: function (data) {
console.log(data)
}
});
};
When I call getLinks(), I get ReferenceError: processLinks is not defined.
External site returns processLinks({"mark": "dog", "number":"33"});.
Thank you for any help

Google API url use by javaScript

I have this url as json object which is provided by google API.
https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script&callback=listEvents
I want to use javaScript and want to see complete json object.This is my script code , but it doesn't work. please help me , i am struggling from 3 days.
function listEvents(root){
$.getJSON('https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script&callback=listEvents', function(data) {*/
alert(root)
});
}
function init() {
listEvents();
}
window.onload = init;
Thanks in advance
You are giving a callback option in url that is again a call to listEvents(). That is not correct. Use your own success callback. this will work:
function listEvents() {
$.getJSON('https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script', function (data) {
console.log(data)
});
}
Demo example
The output is in JSONP format and not JSON
JSONP({JSON})
Jquery JSONP: http://learn.jquery.com/ajax/working-with-jsonp/
(function($) {
var url = 'https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script&callback=listEvents';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'listEvents',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
BTW your Google Daily Limit Exceeded

AJAX - Cross-domain don't work

I was reading many things about that the json is great replacement for XMLHttpRequests. I tried it and it don't works:
$.ajax({
crossDomain: true,
url: settingsURL,
type: "POST",
dataType: 'JSONP',
parseAsHtml: true, cli: 'help',
success: function(data) {
data=$(data).find('div#TestDivContent');
$('#TestDivContent').append(data);
},
error: function() {
$('#TestDivContent').append("<p>Can't Connect</p>");
}
});
and im getting...
Uncaught SyntaxError: Unexpected token <
Please Check the code below that is working like a charm in Cross Domain ().
if You Have control of both the Domains i.e., Domain1.com & Domain2.com
//Ajax Script in Domain1.com
//No Conflict is the code snippet from my sample code You can delete it if not required no issues
<script type="text/javascript">jq1102 = jQuery.noConflict( true );</script>
<script type="text/javascript" >
function jsonp(n){
//GET Response is Here
alert(n);
}
jq1102(function(){
jq1102.ajax({
type: "GET",
dataType: "jsonp",
url: 'http://domain2.com/ClientSiteApi/',
crossDomain: true,
complete: function(data){
//Any Action You Like to Trigger After Complete
},
error: function(jqXHR, textStatus, ex) {
//Nothing to Change Here
}
});
})
</script>
Response from Domain2.com
echo 'jsonp("hello")'; //You Can place JSON string in replace of the Hello String

Revealing module pattern method echo undefined

I wrote this function as revealing module pattern, but when I call the get method in console by metadataModule.get(); it echoes undefined in console.
var metadataModule = function () {
var metadataurl = 'http://farskids326.com/data.json';
function getMetadata() {
console.log("Metadata Function Called")
$.ajax({
url: metadataurl,
dataType: "json",
success: function (data) {
console.log(data);
}
});
}
return {
get: getMetadata,
};
}();
Where did I made a mistake in this code?
When you are working in the console, the return value of the last expresion is echoed after any command. The method you are using doesn't have an explicit return value. So, that may be why you see undefined.
This probably means that your ajax call is encountering an error. Try changing it to log when either success or error happens, like so:
$.ajax({
url: metadataurl ,
dataType: "json",
success: function(data){
console.log('called success!');
},
error: function(jqXHR, textStatus, errorThrown){
console.log('called error!');
}
});
Then, when you run your code, you should see exactly which callback is being executed. Hopefully, that will give you a good starting point for debugging the issue.
The getMetadata function returns nothing so yes, it will output undefined. for it to respond with the content of the JSON you need to make the ajax call synchronous and return the value you get.
var metadataModule = function () {
var metadataurl = 'http://farskids326.com/data.json';
function getMetadata() {
console.log("Metadata Function Called")
var content = {}
$.ajax({
url: metadataurl,
async : false,
dataType: "json",
success: function (data) {
content = data;
}
});
return content
}
return {
get: getMetadata,
};
}();

Categories

Resources