I am trying to use JQuery to access Transloc's API (https://www.mashape.com/transloc/openapi-1-2#!documentation). I have not used JQuery or this API before. If anyone could shed some light on this I would appreciate it.
I can't test this as I don't have a key but it should work in theory.
Using the endpoint you linked to. Change the url for what you need.
$.ajax({
type:"GET",
beforeSend: function (request)
{
request.setRequestHeader("X-Mashape-Authorization", <mashape-key>);
},
url: "https://transloc-api-1-2.p.mashape.com/agencies.json?agencies=12%2C16&geo_area=35.80176%2C-78.64347%7C35.78061%2C-78.68218&callback=call",
processData: false,
success: function(data) {
//use data
},
error: function(data) {
//handle error
}
});
Related
I'm making a simple ajax call as the following;
var getPrevious = function(){
console.log('ajaxing');
$.ajax({
type: 'GET',
dataType: "json",
url: 'http://'+DOMAIN+'/previous',
success: function(data){
console.log(data);
}
});
}
Whenever I run this function it's not returning success. What am I doing wrong; here's the url I'm trying to get the json data from; http://107.174.82.43:3001/previous
I'm new to making calls using $.ajax but it seems pretty straight forward, although I have no idea what I'm doing wrong.
To debug it, use error option
var getPrevious = function(){
console.log('ajaxing');
$.ajax({
type: 'GET',
dataType: "json",
url: 'http://'+DOMAIN+'/previous',
success: function(data){
console.log(data);
},
error: function(err){
console.error(err);
}
});
}
If you are running your app on a different from provided domain/port host, than you will face CORS issue. Your should host your js app either on same domain as your back api, or add js app domain to back-end whitelist.
I have a problem with getting number of shares of my custom url. Im trying this code:
$.ajax({
type: "POST",
url: "http://graph.facebook.com/https://www.myurl.com/somelink/?callback=?",
processData: true,
contentType: 'application/json',
success: function(r){
var fbjson = JSON.stringify(r);
console.log(fbjson);
}
});
I've tried also:
$.getJSON('http://graph.facebook.com/https://www.myurl.com/somelink/?callback=?'), function (data) {
fbjson = JSON.stringify(fbjson);
console.log(fbjson);
};
When I paste url into browser I recieving JSON with all needed information, but when I am using $.ajax etc. I am recieving totaly different json or some error that I should use XMLHttpRequest or some other serwer status information. Important knowladge may be that I am using https://.
Anyone know how to correctly get FB share count for custom URL ?
PS Be understanding for me, this are my first steps in JS, thanks :)
For the json might be cooke issue. your browser have cookie for that URL but in ajax you need to pass that cookie !
Why you are counting share count manually ?
There is lots of share tools is available for fee !
example : http://js-socials.com/
I think this plugin will give you what you need !
Thanks
I've solve it. This script work correctly ;)
(function fbcount() {
var token = 'token here',
url = 'url here';
$.ajax({
url: 'https://graph.facebook.com/v2.7/',
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, id: url},
success: function(data){
$('.facebook-scount').append(data.share.share_count);
},
error: function(data){
}
});
})();
Thanks!
I'd like to create a google-docs add-on that sends an ajax call to a webhook.
I've tried the below, but I get the following error
Error
ReferenceError: "$" is not defined. (line 5, file "")
Code
function myFunction() {
var arr = 'data'
$.ajax({
url: 'webhook_url',
type: 'POST',
data: arr,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function () {
alert('Success');
}
});
}
If ajax can't be used here is there any other way to make a request to a server-side resource
This is an OLD question, so I don't know if you still have the issue, but, from the error you're getting Jquery is either not added, or not available.
You could try doing it with vanilla js, see this link for a walkthrough: https://www.sitepoint.com/guide-vanilla-ajax-without-jquery/
we developed restful service using asp.net MVC4 web API,calling that service from PostMan is working fine,but will calling methods from Html page using JavaScript it shows error, plz any one help me
<script type="text/javascript">
$(function()
{
$("#btnShow").click(function()
{$.ajax({
type:"GET",
url: "http://eprescription.smartelectronicprescriptions.com/api/login",
contentType: "application/json; charset=utf-8",
data: '{}',
beforeSend: function (xhr)
{
xhr.setRequestHeader ("Authorization", "Basic", encodeunpwd());
},
success: function (msg)
{
alert("Success");
},
error: function (msg)
{
alert("error");
}
});
});
});
Because you are using a FQDN for the url, this is probably dropping in a jsonp request which is what is causing the error.
I can only assume this however as you haven't stated the error, have you tried looking at an example like this:
https://learn.jquery.com/ajax/working-with-jsonp/
It might help you get started on fixing it
$(document).ready(function() {
$.getJSON('https://jira.atlassian.com/rest/api/latest/project?callback=?', function(data) {
console.log("success");
});
});
Why this code is not working? Its not giving error also in browser. But a project file is being downloaded as script in Chrome as shown by Inspect Element tool. How can I get data from the file?
It looks like Atlassian use jsonp-callback instead of callback as the parameter in a query string for JSONP callbacks.
See here.
I would suggest you configure your JSONP-call with the jQuery.ajax API like:
$(function() {
$.ajax({
type: "GET",
url: "https://jira.atlassian.com/rest/api/latest/project",
dataType: "jsonp",
jsonp: "jsonp-callback",
data: { /* additional parameters go here */ }
}).done(function(data) {
console.log("success");
});
});
The option jsonp renames the JSONP-callback parameter as #mccannf suggested from the API.
Also, for future reference, you might consider using the jqXHR object to add error-handling functionality, so you can tell if the JSON request is failing. See jQuery's reference (http://api.jquery.com/jQuery.getJSON/)
$(document).ready(function() {
var jq = $.getJSON('https://jira.atlassian.com/rest/api/latest/project?callback=?',
function(data) {
console.log("success");
})
.error(function() { console.log("error occurred"); });
});