google places api error with jquery ajax call... html_attributions - api

I'm using the new google places api with jquery/ajax. When I run this code:
$.ajax({
url: "https://maps.googleapis.com/maps/api/place/search/json?location=40.7834345,-73.9662495&radius=50&sensor=false&key=Your_API_KEY_HERE",
dataType: "jsonp",
data: {
name: 'rogue'
},
success: function( data ) {
console.log(data)
}
});
I get this error: invalid label html_attributions []; I think this is preventing me from seeing the output object in the console, although I can see the response coming back fine in the json tab in firebug

It seems like the places api does not support ajax so far.
It not enough that the server responds with proper JSON. The answering server has to support JSONP and surround the JSON answer with a callback generated by jQuery. The response must look like that:
jQuery17101705844928510487_1324249734338({"data":"whatever"});
The hack that JSONP does, is to interpret the response as script, because the script-Tag is not affected by the Same-Origin-Policy. Without the callback you have no chance to do that in the browser.
If its not supported you have to do the requests from your server..
Server-Example with PHP:
<?php
header("Content-Type:text/javascript"); // avoid browser warnings
$request = new HttpRequest("http://programmingisart.com/json-data-source.php", HttpRequest::METH_GET);
$request->send();
$json_data = $request->getResponseBody();
// wrap the data as with the callback
$callback = isset($_GET["callback"]) ? $_GET["callback"] : "alert";
echo $callback."(".$json_data.");";
Client-Example with jQuery:
<div id="json-result"></div>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
dataType: "jsonp",
url: "jsonp-wrapper.php",
success: function(data) {
$("#json-result").html(JSON.stringify(data));
},
error: function() {
alert("error");
}
});
});
</script>
You can replace the PHP-code with any other server-platform and do the required steps.
HTTP-Request to a JSON source
Wrap the JSON as with a callback-function

Related

Axios POST request not working when passed object

I am trying to do a post request in a vue js app using axios to a local API and the response is returning empty data. The post request on API is working fine using Postman tool. Below is my code
var the_data = {
title: 'This is title',
description: 'this is description'
}
axios.post('/api/snippets/insert', the_data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
On the API end, I am using a simple PHP script and printing whole $_POST request data using this code
var_dump($_POST);
But this is returning empty array.
I was running into this as well. Axios does not send the POST data in the form you're expecting. You need something like http://github.com/ljharb/qs and then use axios.post('/api/snippets/insert', Qs.stringify(the_data)). Please note this build on cdnjs uses Qs, not qs.
Alternatives to qs would be e.g. JSON.stringify() or jQuery's $.param().

Retrieving Auth0 Management APIv2 Token

As outlined in the Auth0 documentation, I am attempting to retrieve a management api token by making a POST request using jQuery from my Ember App:
getToken() {
let settings = {
"async": true,
"crossDomain": true,
"url": "https://brittshroyer.auth0.com/oauth/token",
"method": "POST",
"headers": {
"content-type": "application/json"
},
"processData": false,
"data": "{grant_type:client_credentials,
client_id: 123,
client_secret: fakesecret123,
audience: https://brittshroyer.auth0.com/api/v2/}"
}
$.ajax(settings).done(function (response) {
return console.log('RESPONSE', response);
});
}
As instructed, I have created and initialized a 'Non-Interactive Client' and I have whitelisted 'localhost:4200' (since that is where I am running my Ember App) in the Allowed Origins (CORS) section in the configuration of my client within Auth0. Still, I am running into the following CORS error upon making the POST request:
I'm fairly familiar with CORS, but clearly not familiar enough. Do I need to add a certain header to the payload? Is there a configuration step within Auth0 I am missing? Any help would be awesome.
This code works for me - please just replace with your TENANT, client_id value, and client_secret value.
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</head>
<body>
<script>
var settings = {
async: true,
crossDomain: true,
url: "https://{{TENANT}}.auth0.com/oauth/token",
method: "POST",
headers: {
"content-type":"application/json"
},
"data": "{\"client_id\":\"Z7HtXBOBPXXXXXXXQcJ3Ma\",\"client_secret\":\"FPWb45XXXXXX96U5XmBUZkUXXXXODDJ88NY\",\"audience\":\"https://brittshroyer.auth0.com/oauth/token\",\"grant_type\":\"client_credentials\"}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
</script>
</body>
</html>
In your Client (not API) on the Auth0 Dashboard, remember to add the Allowed Callback URLs (which I believe you are already doing). eg.
Where I think your code went wrong, was somehow it was malformed JSON. In the data section of the JSON snippet above, notice how the value is escaped. This is similar to the code snippet Auth0 offer when click the Try button in the APIs section of the Dashboard.
Note on quick testing: I had npm module serve installed, so just named my snippet above temp.html and ran serve from the same folder. It is then served at http://localhost:5000/temp.html. You can see the output by refreshing the page and viewing the console in web browser developer tools.
Let me know if you have any problems, and please mark this answer as correct if it helps you solve your problem! :)

callback in dojo after loading google oauth client

For loading the google oauth client lib we have to use this script tag
<script src="https://apis.google.com/js/client.js?onload=load"></script>
where the load method will be called after client.js is loaded.
i am using dojo in my application
How can i load this using dojo?
i have tried with dojo/request/script but the callback method is taken by the dojo which is not able to modify
any help how i can do this,
Thanks
call back is sent to the deferred then parameter::
require(["dojo/request/script", "dojo/dom", "dojo/dom-construct", "dojo/json", "dojo/on", "dojo/domReady!"],
function (script, dom, domConst, JSON, on) {
on(dom.byId("startButton"), "click", function () {
domConst.place("<p>Requesting...</p>", "ret");
script.get("http://ajax.googleapis.com/ajax/services/search/web", {
jsonp: "callback",
query: {
"v": "1.0",
"q": "internet kittens"
}
}).then(function (data) {
//Call you function here, or deal with data
domConst.place("<p>response data: <code>" + JSON.stringify(data) + "</code></p>", "ret");
});
});
});
Fiddle::http://jsfiddle.net/D49GP/
UPDATE
You will not be able to use the normal dojo syntax for this one. The problem is that when dojo creates the callback for the then, it creastes the call back function in object.method format. This does not work because google is using window[nameoffunction] for the call back.
So since you can manually add parameters for the script IO. use below:
script.get("https://apis.google.com/js/client.js", {
//jsonp: "onload",
query: {
onload:<callbackfunction>
}
})

Call controller of a project from a diff project using jQuery Ajax

Can we call a controller of one project from another project using Jquery Ajax, MVC4?
If we can, how to do that?
I have tried the below but it is not working:
$.ajax({
url: "/test/Testing",
dataType: "json",
data: {
//We need to send some in future...
},
success: function (data) {
$("div[data-stage = \"stagerWidget\"]").html(data.results[0]);
return;
}
});
This is solved by specifying type of request (get or post).

Ext.data.JsonP.request timeout

I have written a controller. When I click the button from view, this controller generic 'button' is called as follows:
'button': {
tap: function() {
Ext.data.JsonP.request({
url: 'http://download.finance.yahoo.com/d/quotes.csv',
params: {
s: '^BSESN',
f: 'nsl1op'
},
callbackKey: 'callback',
scope: this,
success: function( res, req ) {
Ext.example.msg('Sucess!', 'CSV file successfully generated.');
Ext.data.StoreManager.get('Files').load();
},
failure: function( res, req ) {
console.log('Failed to load csv file.');
}
});
....
....
....
It timesout and failure is called "Failed to load csv file."
The original URL I am using is "http://download.finance.yahoo.com/d/quotes.csv?s=^BSESN&f=nsl1op".
I would like know where I am going wrong.
You're requesting a CSV file via JSONP.
The file will be injected into the DOM via a '' tag (which is how JSONP works), but since it's not a valid JavaScript file, your callback (callbackKey: 'callback') is never executed, so Sencha Touch will fire the timeout handler, seeing the callback has not been fired by the injected <script> tag.
You probably need to change the URL to something that is actually JSONP (iow valid JavaScript wrapped in a callback), not a CSV file.