JSON response from URL - html

Hey I'm trying to retrieve data from a json file I have hosted on my server but it's not working, I'm not sure what is wrong as I'm not the most well versed in this topic. Any tips are appreciated!
$(document).ready(function () {
$("#button").click(function () {
$.getJSON({
type: "POST",
url: "some URL will go here",
success: function (result) {
$("#div1").append(result);
}
});
});
});
heres the fiddle as well: http://jsfiddle.net/ahuston12/E5SzH/

Open a modern browser like Firefox or chrome and open the developer tools. Navigate to the page that contains the above code and monitor the "network" tab to see the related http request triggered via ajax. You can then see the request + response including header, body and return codes. This should help to figure out the problem.

You should show us what's going on the server side.
It doesn't seems to be problems on this jquery code so i guess it's from server.
You can check the response value to see the problem :
on chrome ctrl+j -> network. You'll see your request, click on it to see details like the server's response.

If you have FireBug installed in your Mozilla Firefox open it, in that you can see Net Tab . If you click that you can able to see what kind of response you are getting. I ahve attached the Image For your reference. And for getting a JSON result use "result.d".

The url that you are using doesn't return a json response, it returns a javascript file that charge a json result into a variable, because of that you can't get the result.. if you try with other site that returns information in json format like this you are going to get the data:
$.getJSON("http://headers.jsontest.com/", function (result) {
console.log(result);
}
);

Related

How can I include a hyperlink as a parameter in Express?

I'm trying to send a hyperlink (such as: "http://google.com") as a parameter to my Express server script. My current script looks like this:
var app = require("express")();
app.get("/new/:link(*)", function(req, res){
var link = req.params.link;
res.end(JSON.stringify({
site: link
}));
});
app.listen(process.env.PORT || 3000, function(){
console.log("Listening...");
});
This is just a test to see if I can get it working so I can build something bigger on top. The idea is that I can send a link and receive the link in JSON. However when I try to go to the site with the the link as parameter, my browser want to save a file called "google.com" and it doesn't receive any JSON from the server.
I know it's possible to do this without changing anything about my browser but I don't know how. Anyone has any ideas?
Ok, so I have accidentally fixed my problem.
Apparently i had to write "res.send(...)" instead of "end". It now works perfectly although I don't really understand why.

Flask send_file as attachment not working

I'm using flask's send_file method to try and make the browser download a .txt file. Problem is the browser does not download anything.
Here's my python function:
#app.route('/download_zip', methods=['POST'])
def download_zip():
file_name = 'test.txt'
return flask.send_file(file_name, as_attachment=True, mimetype='text/plain')
Here's my jQuery function that triggers the POST request:
function batchDownload() {
$.post('/download_zip', {
file_name: 'temp.zip'
}).done(function(data) {
alert(data);
}).fail(function() {
alert('Error. Could not download files :(');
});
}
Funny thing is the alert(data) in the .done(...) callback displays the file's content to the browser. So the browser is receiving the file content but just not downloading it.
Any ideas?
Thanks in advance!
EDIT
Added a form to the page:
<form id="download"></form>
And added this to the .done(...) callback:
$form = $('#download');
$form.submit();
I'm guessing I need to somehow link the data (file) returned by the server response to the POST request?
This is not a flask related question.
It's how browsers and javascript works.
You're downloading the file in Ajax so the result is passed to your Ajax callback.
You want instead to make the browser download data in his usual way.
To do so you must use a form and call the .submit() method on it.
Just create the form in the page with hidden fields and submit it in the javascript function. It should do the trick.

Google Chrome show ajax response

I am using Google Chrome Developer Tools to try to see the response of some AJAX url's.
The problem is that when I click on the NETWORK TAB, then on the link, then on RESPONSE, I see this text : "THIS REQUEST HAS NO RESPONSE DATA AVAILABLE".
I have been using FIREBUG and I am 100% sure there is a response from that page.
Can somebody help with this ?
Thank you !
You can try manually checking if there's a response or not
So, generally when dealing with ajax, in most cases we use the POST, You can create a 'same structured' page to handle same input/response but using Get method and print the output data as normal.
This way you can see if there's any response/errors in your script very easily

Phonegap Ajax Post to PHP live server not posting through IOS

I see there are several posts on this issue, however none of the answers are very clear or are working for me. I have a simple JQuery Post that works great in a browser, but doesn't post to the PHP page on the server through the native app on IOS. Is there a handler I can add to make it work or is it a whitelist issue? Either way is there a good resource or simple instructions to fix the issue? See my code below.
$(function() {
$('#RFQ, #sQuote, #sQuote1, #sQuote2, #sQuote3').submit(function() {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function(data) {
$('#RFQ, #sQuote, #sQuote1, #sQuote2, #sQuote3').html( $("#formResponse").html() );
}
});
return false;
});
});
Make sure you modified your file Cordova.plist correctly to allow external access.
Open your file Cordova.plist and check that you made the following:
1 - Set OpenAllWhitelistURLsInWebView to YES
2 - Right click on ExternalHosts -> Add Row
3 - Set the String value of your new added row to *, so you should have your new line look like this:
Item0 String *
Normally, you would replace * with the external URL you want provide access to. But, to make sure that the problem really comes from the whitelist, we will use *.
For more information about domain whitelist, check the online doc: http://docs.phonegap.com/en/2.1.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide
Hope this helps.
Let me know if this works for after modifying your Cordova.plist file as described above.

Using AJAX and return a picture

I have a problem receiving and opening a picture via AJAX.
If I call the following page:
http://127.0.0.1:8889/ex?sql=SELECT+Image+FROM+Persons+WHERE+Number+Like+%27%2501%27
a picture is displayed from a blob field in IE8.
Now I would like to open this into a div after someone pressed a key (using AJAX)?
Trying to use xhr.responseText does not work (I get an error. Using it on a text response works). So it seems that my problem is to grab the result from the ajax request.
How can I do this?
Some code and the error message:
var picReturn = xhr.responseText;
=> Could not continue due to the following error: c00ce514
You have three options:
Place the resultant data in an iframe. Not very practical.
Take the result and place it in am image source as a data:uri. Not supported in older browsers and limited to 32/64Kb depending on the browser.
Skip the AJAX and write a web service and use that as your url. This is the best option.
You don't say what language you're using server-side but you essentially want to open a web response, set the header to "image/jpeg" and return your stream.