Problem
I'm trying to make an AJAX call to an API for river data, but I'm having trouble getting the JSON object to appear in my console. Instead, I get Uncaught ReferenceError: jquery21309662145180627704_1432235037636 is not defined
Update #1 - Added in missing AJAX call code
AJAX Call
$.ajax({
url: 'http://opengov.brandon.ca/OpenDataService/default.aspx?format=jsonp&dataset=riverlevel&columns=Date',
type: 'GET',
dataType: 'jsonp',
success: function(result){
console.log(result);
}
});
API documentation: http://opengov.brandon.ca/api.aspx
Query String:
?format=jsonp&dataset=riverlevel&columns=Date
Going to the url for the AJAX call, I see this:
jsonpcallback([
{
"Date" : "19/05/2015 12:26:05 PM",
"River Level (ft)" : "1170.16000",
"River Level (m)" : "356.66477"
},
{
"Date" : "15/05/2015 9:01:20 AM",
"River Level (ft)" : "1170.51000",
"River Level (m)" : "356.77145"
},
{
"Date" : "14/05/2015 9:08:09 AM",
"River Level (ft)" : "1170.84000",
"River Level (m)" : "356.87203"
},
The API you are calling isn't implementing JSONP properly.
The callback name is case sensitive, but the API is converting it to all lower case in the response. This is a problem because the name that jQuery will generate for you starts with jQuery (with a capital Q).
In order to hack around this, you need to generate your own callback name (instead of letting jQuery do it for you) and ensure that it doesn't include any capital letters.
function callbackName() {
return "jquery_callback" + Date.now();
}
$.ajax({
url: 'http://opengov.brandon.ca/OpenDataService/default.aspx?format=jsonp&dataset=riverlevel&columns=Date&callback=?',
type: 'GET',
dataType: 'jsonp',
success: function(result) {
console.log(result);
},
jsonpCallback: callbackName
});
Related
I am working on a Java application using Struts 1.2. I am facing a blocking error when I make an AJAX call to a Struts action.
The struts action, getInfos.html, is called successfully but after that when I make the AJAX call I get the following error in the console:
Invalid Character/parsing error
The data variable is a correct JSON format. Why would it trigger this error?
I've gone through all the similar questions online but I don't know why it's triggering an invalid character error.
$.ajax({
type: "POST",
url: "getInfos.html",
dataType: "json",
async: false,
cache: false,
data: {
Code: "code1",
type: "type",
mand: "mand",
signature: "signature"
},
success: function(data) {
console.log('succes');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('my error is : ' + errorThrown);
}
});
In the execute method that is handling the ajax request, i am calling the attributes using the request
final String code = (String) request.getAttribute("code");
final String signature = (String) request.getAttribute("signature");
final String type= (String) request.getAttribute("type");
/*
Making a call to a webservice using the attributes bellow,
using **response** Object
*/
if (reponse != null &&
(CodeReponseHttp.OK.equals(reponse.getCodeReponse()))) {
jsonObj.put(SUCCESS_CALL, true);
} else {
jsonObj.put(SUCCESS_CALL, false);
}
return new JsonResult(jsonObj);
But they are set to null; which means that the ajax data is not passed into the request, when I debug the execute method and I explicitly set values to these attributes everything works fine.
new JsonResult(jsonObj) is a generic class with a constructor that accepts a JSONObject
Like Rory McCrossan Comment it could be the response you got is not a json and your code expect a json response
When i comment dataType param it work fine
$.ajax({
type : "POST",
url : "getInfos.html",
//dataType : "json",
async: false,
cache: false,
data: JSON.stringify({
Code : "code1",
type : "type",
mand : "mand",
signature : "signature"}),
success : function(data){
console.log('succes');
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
console.log('my error is : ' + errorThrown);
}
});
The problem had been solved, after debugging, the response type was not a JSON since there is a redirection to an error page if an exception is thrown, the exception was thrown because the data attributes were null, and it turned out that they are parametres not attributes, so getting the parameters solved the problem.
request.getParameter("code");
thank you all for your collaboration.
For below JSON
{
"partnerNameListBeanStruts2Map": [
{
"firstName": "sachin",
"partnerId": 123
},
{
"firstName": "Ankit",
"partnerId": 234
}
]
}
What code should I wright to done jQuery autocompleter.
Here is my code.
Here I want autocomplete element's value is like sachin OR ankit and id is like like 123 OR 234 is id of element.
$(document).ready(function() {
$(function() {
$("#search").autocomplete({
source : function(request, response) {
$.ajax({
url : "list.action",
type : "POST",
data : {
term : request.term
},
dataType : "json",
success : function(data)
{
****What should I write here to work my code?****
}
});
}
});
});
According to the doc, You should return your data with the response callback function.
A response callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.
$(function($) {
$("#search").autocomplete({
source : function(request, response) {
$.ajax({
url : "list.action",
type : "POST",
data : {
term : request.term
},
dataType : "json",
success : function(data)
{
***response (data) ;***
}
});
}
});
});
I'm using jquery API - jquery DataTables and I have this code snippet :
oSettings.aoDrawCallback.push({
"fn": function(){
},
"sName": "user"
});
Inside the body of the function I want to execute an Ajax request. when I write it drectly here like so:
"fn": function(){
$.ajax({
url: "url",
type: "POST",
async: false,
data: data,
success: function (data) {
console.log(data);
}
}),
There is more that is just an example to show the way everything's work fine. Then I create my own function :
function initCredits(id, inputVal, chkSelected) {
console.log(id);
$.ajax({
url: "URL",
type: "POST",
async: false,
data: data
success: function (data) {
}
})
}
and try to assing it do fn like so:
oSettings.aoDrawCallback.push({
"fn": initCredits(id, inputVal, chkSelected),
"sName": "user"
});
which gives me an error Uncaught TypeError: Cannot read property 'apply' of undefined. Now the text comes from the jquery DataTables API but there may be only two reasons I can think of that may break my code, since it's working befor taking it to outer function. First - I'm tryng to assing the function in a wrong way and second - as you may see I need three variables for my ajax request (id, inputVal, chkSelected) which are collected from the function where I'm doing this :
oSettings.aoDrawCallback.push({
"fn": initCredits(id, inputVal, chkSelected),
but the console log shows that the values are correct so I think this is less likely to be the problem, but still I consider it.
This:
"fn": initCredits(id, inputVal, chkSelected),
… calls the function and assigns the return value.
To assign the function, just do:
"fn": initCredits,
This is my Ajax Request:
$.ajax({
type: 'POST',
url: "http://xx.json",
//data: '{id:id}',
//data: '{providername:providername}',
crossDomain: true,
dataType: 'jsonp',
jsonpCallback: 'loadData'
});
ok my request itself if failing coz i need to provide data in data variable above in request not sure how should I send my data to JSON to access the contents.following is json
{
"loadData" : {
"Facebook": [
{
"email" : "karthim1982#yahoo.com",
"first_name" : "Karthick"
},
{
"email" : "mallika132-iit#yahoo.co.in", "first_name" : "Mallika"
}
],
"Google" : [
{
"email" : "jameson42#gmail.com","first_name" : "Jameson"
},
{
"email" : "hariikriishnan#gmail.com","first_name" : "hari"
}
]
}
}
Please do check if anything wrong with JSON.
How can I access the Facebook 1st and 2nd Email or Google's first Email or First)name attribute
If you are asking for jsonp using jQuery, you will get a parameter called callback in the GET request, something like:
http://your.url/out.json?callback=jQuery123456789
What you need to do on the server-side is wrap the ouput in that callback, so instead of:
{"a": 1, "b": 2}
You need to provide your response like so:
jQuery123456789('{"a": 1, "b": 2}')
In your case, it looks like you're renaming the callback to loaddata. Keep in mind that jQuery will build this function for you, you don't need to define it. What you do need to define is the success callback that the data will be passed to.
For JSONP, jQuery loads the response not as an XMLHTTPRequest but as a <script> tag and secretly sets up that callback on the page. This is banking on you having control over the remote service.
I am baffled by this for very long now. I want to gain this precious knowledge of making JSON call properly. Help me Humans.
So I'm making a call exactly like this:
$.ajax({
type : "POST",
url : "http://quote.mythicalQuotes.com/unicorn/service/historical/json?callback=callme&symbols=APPL",
dataType: "text",
cache : false,
data : My_Array,
error : function(request,error){alert(request+" "+error); },
success : function(data)
{
alert("Response" + data);
}//success
}).fail(function(textStatus, errorThrown) { alert("error Error");
console.log("The following error occured: "+ textStatus, errorThrown); });
But it fails and throws 'error' alert. Good Coding!
Now pasting "http://quote.mythicalQuotes.com/unicorn/service/historical/chart/lite/json?callback=callme&symbols=APPL" on my browser URL gives me nice JSON of format:
callme(
{
"SYMB" : [
{
"DESCRIPTION" : "APPL,
"BARS" : {
"CB" :[
{
"lt" : "09-01-2011::20:00:00",
"op" : "16.31",
"cl" : "15.22",
"hi" : "16.45",
"lo" : "14.72",
"v" : "17768019"
},
{
"lt" : "09-02-2011::20:00:00",
"op" : "15.22",
"cl" : "14.22",
"hi" : "19.45",
"lo" : "10.72",
"v" : "17768000"
}
]
}
]
})
So what atrocity am I doing here which is provoking my anger toward this particular Javascript semantics/syntactics?
Couple of reasons I thought which might cause this.
1. Same origin policy.
2. Wrong JSON format being return.
3. Stupidity of my code.
Please help.
This is a JSONP-type response. Add dataType: jsonp to the JQuery AJAX request. Since you're also specifying the callback function explicitly, add jsonpCallback: callme also. See the JQuery docs for more info (scroll down to the "dataType" section).
$.ajax({
dataType: "jsonp",
jsonpCallback: "callme",
// ...
success: function(data) {
alert(data); // should show the JSON: { "SYMB" : ... }
}
});
You mentioned the cross-domain policy; the JSONP spec is a workaround for this policy blocking cross-domain requests. The idea is that, rather than returning data, the server returns a Javascript snippet containing data. The client then executes the returned function to retrieve the data. The JQuery ajax method has built-in functionality to handle all this "behind the scenes".