Load D3 Url With API key - javascript

I am trying to load a dataset from the URL "https://data.raleighnc.gov/resource/5ccj-g2ps.json" but it requires an API key. I have had no luck with D3 or Jquery.
How would I go about doing this so that I can load the dataset in Json format?
I have the following:
$.ajax({
url: "https://data.raleighnc.gov/resource/xce4-kemu.json",
type: "GET",
data: {
"$limit" : 5000,
"$$app_token" : "YOURAPPTOKENHERE"
}
}).done(data) {
alert("Retrieved " + data.length + " records from the dataset!");
console.log(data);
});
It says I have a misplaced "{" but I don't see where.

There are a lot of mistakes in your code...
$.ajax({
url: "https://data.raleighnc.gov/resource/xce4-kemu.json", // didn't you want to get another URL??!?
type: "GET",
dataType: "json",
data: {
"$limit" : 5000, // Does the API require the dollar signs? Looks weird.
"$$app_token" : "YOURAPPTOKENHERE" // Did you actually replace with your API key?
},
success: (data) => {
alert("Retrieved " + data.length + " records from the dataset!");
console.log(data);
},
error: (xhr, textStatus, errorThrown) => {
// error
}
});
Should be working.

Related

Sending variable to PHP with AJAX, receiving JSON back

Using the following jQuery, how can I read through the values of the JSON that's returned? With it how it is, the jQuery doesn't even run as there is an error in: alert("A" + obj.sender[0]);
var session_id = $(this).find(".session_id").val();
$.ajax({
type: 'POST',
url: '../php/read.php',
dataType: "json",
data: {sesh_id: session_id},
success: function (response) {
var obj = jQuery.parseJSON(response);
alert("A" + obj.sender[0]);
},
error: function (response) {
alert("Err: " + response.status);
}
});
The value of response is:
[{
"sender":"email#example.com",
"details":"details1",
"date":"2017-01-04 16:11:04"
},
{
"sender":"someone#example.com",
"details":"details2",
"date":"2017-01-04 16:11:05"
},
{
"sender":"blah#example.com",
"details":"details3",
"date":"2017-01-04 16:11:06"
}]
The issue you have is that your index accessor is in the wrong place as obj is an array, not the sender property, so it should be obj[0].sender.
You also don't need to call JSON.parse() on the response, as jQuery does that for you automatically as you specified dataType: 'json'. Try this:
$.ajax({
type: 'POST',
url: '../php/read.php',
dataType: "json",
data: { sesh_id: session_id },
success: function (response) {
console.log("A" + obj[0].sender);
},
error: function (response) {
console.log("Err: " + response.status);
}
});
Finally, note that console.log() is much more preferable when debugging over alert() as it doesn't coerce data types.

Ajax Error when looping

Im using a mixture of php and javascript/jquery to scrape a websites prices from there webpage, there's no API so unfortunately I scrape the html page and pick up the prices/title from the html(I know this is a really risky way of doing it but its the only way).
Anyway, this is whats going on:
I pull in the external webpage using php file_get_contents()
This method is within a foreach loop, for every external page I want to grab data from.
This is my javascript.
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
console.log('page-{$i}');
$('.page-{$i}').find('.search_result_row').each(function(i, obj) {
//This will give us each individual apps details.
var appTitle = $(this).find('.title').text();
var appPrice = $(this).find('.search_price').text();
var appDiscount = $(this).find('.search_discount').text();
var appDetails = {
'appTitle' : appTitle,
'appPrice' : appPrice,
};
callAjax(appDetails);
var my_delay = 5000;
function callAjax(appDetails) {
$.ajax({
url: 'Upload.php',
type: 'POST',
data: appDetails,
dataType: 'JSON',
success:function(data) {
console.log(data);
},
error:function(jqXHR, textStatus, errorThrown) {
console.log('request failed ' + textStatus + errorThrown);
console.log(appDetails);
}
});
}
});
</script>
Everything works fine for the first URL, and for about half of the urls that I'm grabbing data from. The issue is SOME of the data being sent via ajax is returning the following error
Upload.php net::ERR_EMPTY_RESPONSE
Can any of you help?
Try this. You must stringify your details before you post them as json. This could be a reason for ERR_EMPTY_RESPONSE.
function callAjax(appDetails) {
appDetailsJsonString = JSON.stringify(appDetails);
$.ajax({
url: 'Upload.php',
type: 'POST',
data: {appDetailsJson: appDetailsJsonString},
dataType: 'json',
success:function(data) {
console.log(data);
},
error:function(jqXHR, textStatus, errorThrown) {
console.log('request failed ' + textStatus + errorThrown);
console.log(appDetails);
}
});
}

jQuery Ajax returning 404 when method = post

So I have an ajax script that runs, it looks like this:
jQuery.ajax({
url: 'http://localhost/?page_id=104256',
type: 'POST',
data: { name : 'name2' },
success: function (data) {
alert(data);
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details0: " + desc + "\nError:" + err);
},
});
This runs fine but returns a 404 from the page set as the 'url'
If I remove 'type: post'
Here your method: 'Post', Type is something what you want to get in return like text
jQuery.ajax({
url: 'http://localhost/?page_id=104256',
method: 'POST',
data: { name : 'name2' },
success: function (data) {
alert(data);
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details0: " + desc + "\nError:" + err);
},
});
If type: 'POST' is omitted, jQuery is treating it like a GET request, which it defaults to see the docs, where the resource may not exist therefore resulting in a the 404 you're seeing.
It turns out I forgot to add the name="" parameter in my input types. Doh!

How to update jqx grid using jquery

$.ajax({
url: "RequestHandler?usercommand=showcriticaldatezone&useraction=ShowUserPreferenceDashbord_frm&subCommand=criticaldatezone",
type: 'POST',
dataType: 'json',
data: dateText,
contentType: 'application/json',
async:'false',
cache:'false',
success: function (data) {
var parsedJSON = JSON.parse(data);
var tempdate;
alert("parse json - "+parsedJSON.length);
js= [];
for (var i=0;i<parsedJSON.length;i++) {
alert(" critical date is -:"+parsedJSON[i].CriticalDate);
js.push(parsedJSON[i].CriticalDate);
//tempdate='{"Date":['+'"'+ parsedJSON[i].CriticalDate + '"' + ']}';
}
tempdate='{"Date":'+JSON.stringify(js)+'}';
alert("stringify is -:"+tempdate);
selectedDate=tempdate;
jqxgriddata=data;
$(this).datepicker("refresh");
jQuery("#myGrid").setGridParam({ 'data': data}).trigger("reloadGrid");
alert('sucess!');
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
i am using above code to refresh grid after server response but not success i am using jqx grid Server response is json array i am able to display grid at first time but not able to refresh it please help

Get entire html page as response with javascript

I am doing an Ajax post to a specific page where I either can get an ID as response if everything went as expected or I could get a random html page and a http 400 as response if something went wrong. In the error case I want to open up the entire html page in a new window. I have tried the following but it is not working - it sets the variable data to [object Object] instead of the intended html page.
$.ajax({
type: "POST",
url: postUrl,
    data:'message=' + message + '&' + 'title=' + title,
statusCode: {
400:function(data) {
var newWin = open('','windowName','height=300,width=300');
newWin.document.write(data);
},
},
success: function(json) {
alert("Post success");
},
    error: function(jqXHR, textStatus, errorThrown) {
        alert("Error connecting to page.");
    }
});
Specify dataType : 'html' and change the success function variable. Example:
$.ajax({
type: "POST",
dataType: 'html',
url: postUrl,
data:'message=' + message + '&' + 'title=' + title,
statusCode: {
400:function(data) {
var newWin = open('','windowName','height=300,width=300');
newWin.document.write(data);
},
},
success: function(html) {
alert("Post success");
$("#myDiv").html(html);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error connecting to page.");
}
});

Categories

Resources