I am using this Ajax call to grab a CSV file from the Internet. Currently the way that it is implemented is with a call to yahooapis and a SQL like query of the file.
function getData(qry){
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql',
data: {
q: qry,
format: 'json',
_maxage: 120,
ts: getTS(dateNow)
},
cache:true,
dataType: 'jsonp',
success:function(data){
setFlowData(data);
}
});
}
The qry is similar to something like this:
select * from csv where url IN ('http://example.com/public/zone.csv')
The file is then parsed by a function similar to this:
if(data.query.count > 0){
var queryData = data.query.results.row;
if (queryData.col2 == rt.id) {
rt.lbmp = queryData.col3;
rt.ts = queryData.col0;
}
}
My question is how do I achieve the same functionality without the use of the YQL/SQL call? Is this possible with JavaScript implementation? Thanks for the help.
Absolutely you can process CSV data without any YQL stuff. You can use $.ajax to pull csv data if it is from your own domain or CORS-ready, or through CORS proxy server. There is also jquery-csv(https://code.google.com/p/jquery-csv/) or PapaParse(http://papaparse.com/) to parse CSV data.
Related
i am new in j query ajax.i don't have idea how to fetch data from rest API using j query ajax. here i have created JS function to check whether passing URL working proper or not.
kindly help me.
<script type="text/JavaScript">
$.ajax({
type: "GET",
dataType: "jsonp",
url: "https://www.propertyfinder.ae/en/find-broker/ajax/search?page=1",
success: function(data){
alert(data);
}
});
</script>
You cannot do a cross domain call using ajax. what you can do is to do a php call like :
$website = file_get_contents('http://google.com');
and store it in a text file and access that using your ajax on your server using the same ajax file you have.
i had similar case that i had a php call on scheduler to grab text from url every minute and update a json file. then simply use :
$.getJSON( "ajax/test.json", function( data ) {
//your function
});
or use ajax! whichever you fancy
I'm trying to implement a custom suggestion engine using jquery.
I take the user input, call my codeigniter v2 php code in order to get matches from a pre-built synonyms table.
My javascript looks like this:
var user_str = $("#some-id").val();
$.ajax({
type: "POST",
url: "http://localhost/my-app/app/suggestions/",
data: {"doc": user_str},
processData: false
})
.done(function (data)
{
// Show suggestions...
});
My PHP code (controller) looks like this:
function suggestions()
{
$user_input = trim($_POST['doc']);
die("[$user_input]");
}
But the data is NOT posted to my PHP :( All I get as echo is an empty [] (with no 500 error or anything)
I've spent 2 days looking for an answer, what I've read in SO / on google didn't help. I was able to make this work using GET, but then again, this wouldn't work with unicode strings, so I figured I should use POST instead (only this won't work either :()
Anyone can tell me how to fix this? Also, this has to work with unicode strings, it's an important requirement in this project.
I'm using PHP 5.3.8
Try using the $.post method, then debug. Do it like this:
JS
var user_str = $("#some-id").val();
var url = "http://localhost/my-app/app/suggestions";
var postdata = {doc:user_str};
$.post(url, postdata, function(result){
console.log(result);
});
PHP
function suggestions(){
$user_input = $this->input->post('doc');
return "MESSAGE FROM CONTROLLER. USER INPUT: ".$user_input;
}
This should output the message to your console. Let me know if it works.
For those interested, this works for me, even with csrf_protection being set to true
var cct = $.cookie('csrf_the_cookie_whatever_name');
$.ajax({
url: the_url,
type: 'POST',
async: false,
dataType: 'json',
data: {'doc': user_str, 'csrf_test_your_name': cct}
})
.done(function(result) {
console.log(result);
});
I am trying to write a script that allows posting to a JSON file. For some reason the process is succeeding, but the JSON file isn't being written to.
Here is my jQuery code:
$(function() {
var data = {
name: 'cool',
drink: 'cool2',
};
$.ajax({
type: "POST",
url: '/api/orders',
dataType: 'json',
async: false,
data: JSON.stringify(data),
success: function() {
alert("Thanks!");
}
})
});
Here is my JSON code (/api/orders)
[
{"id":1,"name":"Ben","drink":"Americano w/ Creme"},
{"id":2,"name":"Ben2","drink":"Americano w/ Creme2"},
{"id":3,"name":"Ben3","drink":"Americano w/ Creme3"}
]
I can't figure out why Chrome is saying it is succeeding, but the code isn't posting to the JSON file.
The problem isn't with your JavaScript. The JSON.stringify just concerts the data to a JSON string and passes it to the url. Your problem will be in the Url that it is posted to /Ali/orders. That looks like a directory to me instead of an API to handle the string. I could be wrong in that as certain technologies hide that stuff. Still you need to look at where the data is going to.
I've been searching around, but I cannot find an answer, my guess is that my question is not defined very well, so i hope to get some guidance
I'm using turbogears2.2
I'm sending from the client view a JavaScript object through $.post(),
On the server I receive this object (as kw):
{'phones[1][surname]': u'sym', 'phones[2][name]': u'', 'phones[1][phone]': u'5498498', 'phones[0][phone]': u'0564', 'phones[1][name]': u'jhon', 'phones[0][surname]': u'cat', 'phones[2][phone]': u'', 'phones[0][name]': u'bob'}
I'm sending a data from a table with 3 columns
On my server I'm trying to separate the data for each row, but I'm a bit lost here.
how can I split that dict into different rows of data?
Doing
import json
json.loads(str(kw))
Fails
{ValueError}: Expecting property name: line 1 column 2 (char 1)
How can I convert that dict/object to a nested dictionary (or something else)?
Thanks
Thanks for the help Martijn Pieters , I thought I was posting JSON, but i wasn't.
You didn't post JSON; you posted application/x-www-form-urlencoded data that jQuery formatted for the PHP array feature. – Martijn Pieters
Here is my JavaScript, I'm trying out HandsOnTable (jQuery data grid editor)
$("#sendToServer").click(function(){
var tableData = $("#myTable").data('handsontable');
$.post(
URL,
{phones:tableData.getData()},
function(data){
console.log(data)
}
)
})
And that wasn't JSON, even though I thought it was
Anyway, simple fix:
Client side (JavaScript):
var tableData = $("#myTable").data('handsontable');
var jsonData = JSON.stringify(tableData.getData())
...
$.post(URL,
{phones:jsonData},
...
)
And now on the server side (Python):
import json
def getPhones(self,**kw):
phones = json.loads(kw['phones'])
...
And I have the data as a dict of rows, great
Thanks for the guidance
To post JSON, don't use $.post(). Use $.ajax() instead, and set the content type explicitly:
$.ajax({
url: URL,
type: "POST",
data: JSON.stringify({phone: tableData.getData()}),
contentType: 'application/json; charset=utf-8'
dataType: 'json',
success: function(data) {
console.log(response);
},
});
This can especially help if you have a controller that handles JSON directly, such as those provided by TurboGears Web Services.
I am using GeoServer version 2.2.5, and what I try to do is making a AJAX call to get the json string from the output GeoJSON url, for example:
http://localhost:8080/geoserver/sf/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=sf:archsites&maxFeatures=50&outputFormat=json
the javascript I used is like this:
var processJSON = function (data) {
console.log(data);
};
function init() {
//geojson url
var url = "http://localhost:8080/geoserver/sf/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=sf:archsites&maxFeatures=50&outputFormat=json&&format_options=callback:processJSON";
//execuate ajax request to get data
$.ajax({
url: url,
dataType: 'jsonp',
jsonp: 'processJSON'
});
}
I am pretty sure that this method works, because I can get the json object from the url which is from GeoServer version 2.2.4. But it just doesn't work for GeoServer 2.2.5 and later. I read somewhere that says "JSONP support has been disabled by default since it is perceived as a security issue." But I have no idea how to make it work.
Can anyone give me some suggestion on this?
Thank you very much