Trying to get a tweet using JSON. I have the tweet ID but nothing returns. Any ideas?
Here is the code im using
$.getJSON("https://api.twitter.com/1/statuses/show.json?id=204981356940369920", function(data) {
$("#twitter").html(data[0].text);
});
That's returning JSON. Due to the same-origin security policy, you'll need to use JSONP.
So, the call needs to be
https://api.twitter.com/1/statuses/show.json?id=204981356940369920&callback=whatever
If you're using jQuery, you can use
https://api.twitter.com/1/statuses/show.json?id=204981356940369920&callback=?
And jQuery will automagically add the callback
Related
I'm using Turbolinks 5 on the backend, and turbolinks-ios in my iOS app.
When a page loads in a VisitableViewController, I'd like to have the native app receive (or retrieve?) some metadata from the web page, in order to offer some additional UI affordances using native code.
I know I can do this in a roundabout way by setting some <meta> tags on the server side, and then executing JS in the WKWebView and getting at the meta tags that way, but it seems like a hack.
Another option is executing a turbolinks callback message and receiving via addScriptMessageHandler, but that also seems like a hack.
Any help would be appreciated! Perhaps there is a better, sanctioned way to do this? It seems like it would be a common need.
I ended up going with my original idea with the meta tags. Then, what I do is on page load, invoke a message handler like so, passing the contents of those (filtered) meta tags to the handler:
$(function() {
if (typeof webkit != 'undefined') {
var metadata = {};
$('meta[data-scanner-app="true"]').each(function() {
metadata[$(this).attr('property')] = $(this).attr('content');
});
webkit.messageHandlers.ScannerApp.postMessage(metadata);
}
});
Then I simply intercept that handler in turbolinks, and I have the data.
Note: data-scanner-app is a data attribute I add to just the meta tags I want to include.
I'm not even sure how to ask my question.
I know the basics of ruby on rails and I can make a web app. but I now have a project where I want to have a button on a view that sends data to the controller, but I don't want the controller to send me to a different view, instead I want it to send data back to me (maybe in Json format) and populate something in the view with that data retrieved from the database.
Is this possible? If so how would I go about doing it? What tools do I need if this kind of functionality isn't built into rails itself? I don't know anything about it but could ajax help?
Thanks for the guidance!
You have to make it with javascript.
In your case I advise you to use a simple ajax request with jQuery.
http://api.jquery.com/jquery.ajax/
Jquery is a javascript library that allow you to do this.
1) Include jquery in your html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
2) Make the ajax query:
$.ajax({
url: "/api/your_url",
data: {
test: "parameter send to the rails controller and stored into params"
},
success: function( data ) {
console.log(data);
}
});
What you are referring to is called Ajax. You can use XmlHttpRequest in your client side javascript to send an asynchronous request to the server and register a callback function that will be called on the client when the server responds. In the callback function you can update your webpage with the new information.
In rails i need to take a base64 string, and use it to generate a picture in rails. Now i'm having trouble, because i need to interact with AJAX calls (im strictly working on the server side, another guy is doing that client work) to send pictures. So far i've been taking requests in my application by having data transferred through the url (in the AJAX requests) but now im not sure if it's possible to transfer such a huge string through the url. How could i take in the data (like how could he pass it to me) to generate a picture.
Note: i've been using paperclip for my application so far, but now uploading through the form is not an option, it needs to be in an AJAX call where data is passed in a single call.
You're right, most browsers limit the length of a URL. The limit on IE8/9 is 2083 characters. Even if your particular browser has a higher limit, many servers limit the URL length as well (apache's default limit is right around 8k). It would be best to submit the image as a POST request with the data in the POST body.
I would use jQuery to POST JSON data to the server. In the controller, if this is set up correctly, you won't have to do a thing to parse the JSON. ActiveSupport will recognize the content type and parse it out into the params hash automatically.
Actually posting the data will depend on which javascript library you're using. Here's an example in jQuery, which you'd probably want to wire up to the onclick event of a submit button. This assumes you have a named route called process_image. This code would go in your view.
$.post(<%= process_image_path %>, { b64_img: "your_base64_image_data" });
In your controller, you can access the posted data with params[:b64_img]. If you want to return something from the controller back to the client, you can do this in the controller:
render :json => #model_object
And change the jquery call to look like this so you can do something with the return value:
$.post(<%= process_image_path %>, { b64_img: "your_base64_image_data" },
function(data) {
// do something with the data returned by the controller
});
Hope this helps. You can read more about the jQuery post call I used here: http://api.jquery.com/jQuery.post/
Dan
I'm using jQuery getScript in Rails to load an AJAX search on a dashboard page. I just noticed, though, that in addition to properly making the call it's ALSO reloading the entire page (in the background).
I have no idea why this happening.
I checked all my before_filters, all my authentication logic, I tried using different jQuery ajax functions (get, getJSON, etc.), but nothing. it's still reloading the page. also, the two routes are even on different controllers!
Does anybody know what might be going on?
EDIT:
RESOLVED.
I was using an $.ajax({}) function in addition to a $.get() function in order to set a before function. Something in the $.ajax must have been triggering the call, so I simply merged the new functions into one and it resolved my problem.
BTW, though, the xhr.request?, which I discovered in this process, is helpful for detecting javascript calls, and preventing certain actions from responding to javascript.
This jquery javascript was triggering the extra call:
$.getScript(correct_url, function({
$.ajax({beforeSend: function(){}...)}
callback code
)}
You can't use the ajax shortcuts inside the getScript shorthand function like that. The inner .ajax was making its own call. So I simply combined them into one .ajax function
$.ajax({
url: correct_url,
type: 'get',
dataType: 'script',
beforeSend: function(){
}),
success: function (){
<callback code>
}
})
Old hand with Prototype, new to jQuery, and writing a simple app to get a feel for the framework (and because I want to use it). I've got an HTML fragment that I load via AJAX, and I want to stick this at the top of a div, with a slide-in transition animation.
This bit works, and does the prepending bit:
// Get the HTML fragment and stick it at the top of the containing div.
$.post("/create_new_thing", function(data) {
$('#container_div').prepend(data);
});
What I'd like to do, and can't figure out, is animate the newly added HTML fragment with a show() effect.
Any suggestions?
Try something like this...
$('#div').load('file.html').fadeIn("slow");
The load function is better suited to your needs, as it's main purpose is to load HTML from a remote file and inject it into the DOM.
Using the "post" function is better for loading a remote page using a POST request (posting data via a form and returning dynamic data based on your post request).
See below...
$.post("file.php", { name: "superuntitled", time: "2am" },
function(data){
$('#div').fadeIn("slow").append(data);
});
jQuery has no support yet for "PUT" requests. So if you really need to use a put request, I can recommend extending the jQuery functionality with a custom function that adds support for "PUT". That said, there are some work arounds! See here for more details! ... http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery
This is what I use for adding a new post to the list:
success: function(data){
$("#posts-container").prepend(data).children().first().hide().show('slow');
}
This assumes the added element is wrapped in a div and is first in the #posts-container.
I am using this
$.post("yourdocument.php", {parameter:"caucana.com"}, function(data) {
$('#contiene_resultados').prepend(data);
});