Angular $http with bbc radio api - json

I need to get this json from bbc radio:
http://www.bbc.co.uk/radio1/playlist.json
I can get it from POSTMAN, but then I try to get it from my angular app, I've got an error.
XMLHttpRequest cannot load http://www.bbc.co.uk/radio1/playlist.json. No 'Access-Control-Allow-Origin' header is present on the requested resource
Yes, I saw a lot simular guestions on Stack Overflow, but I think this is not a server task to solve.
In my angular code I've got this:
async: function (radio) {
return $http({
method: 'GET', //or JSON
url: api.url + radio + '/playlist.json',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*'
}
})
.success(function (d) {
console.log('done');
})
.error(function (d) {
console.log('nope');
});
}
And this in config:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
But CORS isn't working anyway.

Apparently this playlist and BBC api doesn't support jsonp and have no CORS setup so the solution that I can think from top of my head would be to use i.e. CURL
http://plnkr.co/edit/66yErNbEkONrcC8LjqUV?p=preview
$http.jsonp('http://edeen.pl/curl.php?callback=JSON_CALLBACK').then(function(response){
$scope.playlist = response.data
console.log(response.data)
})
curl.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.bbc.co.uk/radio1/playlist.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $_GET['callback'].'('.$output.')';

Related

AngulasJS HTTP Post request 431 (Request Header Fields Too Large)

I am using AngularJS to send the array element to my Node.js where I need to do some process. Everything works fine when the array size I am sending is small but if I try to use the array which is of 200 sizes then I get the error 431 (Request Header Fields Too Large). How can I resolve this issue? I cannot spit my array.
var data = JSON.stringify({
input : $scope.input,
Input1 : $scope.Input1,
Input2 : $scope.Input2,
Input3 : $scope.Input3
});
$http({
url: "/createEvents",
method: "POST",
headers: {'Content-Type': 'application/json; charset=utf-8'},
params: {data:data}
}).success(function(response) {
console.log(response)
}).error(function(error) {
console.log(error)
});
I am not understanding how to fix this issue.
$http({
url: "/createEvents",
method: "POST",
headers: {'Content-Type': 'application/json; charset=utf-8'},
data:data // check this line.
}).success(function(response) {
console.log(response)
}).error(function(error) {
console.log(error)
});

Connecting to HTTPS web services API with node.js v4.2.6?

I'm looking at connecting to an https web api, I've obtained my token, and my username by receiving an email about it, and there isn't really any sample code to connect to the webservice using node; however there are examples for Java and C#, and based on those this is what I came up with...
/* WEB API: https://www.careeronestop.org/Developers/WebAPI/technical-information.aspx?frd=true */
// UserID: ...
// Token Key: ...==
// Your agreement will expire three years from today on 12/8/2019 and all Web API services will be discontinued,
// unless you renew.
var https = require('https');
var username = '...';
var tokenKey = "...==";
var options = {
host: 'api.careeronestop.org',
port: 443,
path: '/v1/jobsearch/' + username + '/Computer%20Programmer/15904/200/2',
method: 'GET',
headers: {
'Content-Type' : 'application/json',
'Authorization' : '' + new Buffer(tokenKey.toString('base64'))
}
};
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
Unfortunately however, it returns a 401 Unauthorized, so is there anything that needs added to this to get it working? Some headers maybe?
I used this form to submit a request and then looked in the Chrome debugger network tab to see exactly what request was sent.
The authorization header is supposed to look like this:
Authorization: Bearer 901287340912874309123784
You also want this:
Accept: application/json
So, assuming tokenKey is already a string since it appears to have been sent to you in an email, you can change your code to this:
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + tokenKey
}

Angular JS issues sending JSON with $ http

I am having issue sending post json data with angular, from PHP I need to access $_POST['action']
This works
$http({
method: 'POST',
url: ajaxurl,
data: "action=get_employer_jobs",
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
console.log(data);
$scope.jobs = data;
});
This does not work
app.controller('MainController', ['$scope', '$http', function ($scope, $http) {
console.log('MainController Running');
$http({
method: 'POST',
url: ajaxurl,
data: JSON.stringify({action:"get_employer_jobs"}),
headers: {'Content-Type': 'application/json'}
}).success(function (data) {
console.log(data);
$scope.jobs = data;
});
}]);
You need to process your json on the server side differently.
in PHP, I do it like this:
$_REQUEST = json_decode(file_get_contents("php://input"));
The way Angular sends POST data as json strings, you need to take the whole request string, parse it, then you can use it. It won't automatically be in $_POST or $_REQUEST as a nice array until you do this parsing.
I´d the same problem, it looks like PHP doesn´t fill the $_POST array when it´s passed by the $http service.
Instead of using $_POST, try to parse directly the php://input.
php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
var_dump($request)
Then you can access using the object notation $request->my_var

Getting 411 Length required from nginx when making a json call

Working with a new API -- MemberMouse if anyone cares/knows -- and am stuck on an "411 Length Required" response when making a request. I am successfully making the calls to the API with a php function below:
<?php
$inputParams = "apikey={{key}}&apisecret={{secret}}&";
$inputParams .= "email=ivan#email.com";
$apiCallUrl = "http://www.website.com/wp-content/plugins/membermouse/api/request.php?q=/getMember";
$ch = curl_init($apiCallUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $inputParams);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo "RAW Response: ".$result."<br />";
$data = json_decode($result);
echo "<pre>";
var_dump($data);
echo "</pre>";
?>
However I'm building my app in Node.js, and I'm trying to make a comparative call using Node's request library. Here's what I have so far.
var request = require('request');
var options = {
url: 'http://www.website.com/wp-content/plugins/membermouse/api/request.php',
json:true,
multipart: [
{
'content-type': 'application/json',
body: JSON.stringify({
'q': '/getMember',
'apikey': '{{key}}',
'apisecret':'{{secret}}',
'email':'ivan#email.com'
}),
}
],
headers: {
'content-type': 'application/json',
'Connection': 'Keep-Alive',
}
};
function callback(error, response, body) {
console.log(response);
if (!error && response.statusCode == 200) {
console.log(response);
}
}
request.post(options, callback);
And it refuses to make it past nginx with a 411 Length Required Error. Anyone have any idea on what's going on? Thank you.
Gave up after hours of trying, and opted in for node-curl -- though not ideal, as it ties into OS operations, it did provide a nice way to pull the information I need. For anyone who's interested:
var curl = require('node-curl');
var inputParams = "apikey={{}}&apisecret={{}}&email=ivan#email.com";
var apiCallUrl = "http://www.website.com/wp-content/plugins/membermouse/api/request.php?q=/getMember";
var options = {
'POST':1,
'POSTFIELDS':inputParams,
'HEADER':0,
}
curl(apiCallUrl, options, function(error, body) {
console.log(JSON.parse(body.body)['response_data']);
});

Returning JSONP from Symfony2 controller using AJAX call

I'm trying to return JSONP from Symfony2. I can return a regular JSON response fine, but it seems as if the JSON response class is ignoring my callback.
$.ajax({
type: 'GET',
url: url,
async: true,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data)
{
console.log(data);
},
error: function()
{
console.log('failed');
}
});
Then in my controller:
$callback = $request->get('callback');
$response = new JsonResponse($result, 200, array(), $callback);
return $response;
The response I get from this is always regular JSON. No callback wrapping.
The Json Response class is here:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/JsonResponse.php
As the docs says:
$response = new JsonResponse($result, 200, array(), $callback);
You're setting the callback method as the $headers parameter.
So you need to:
$response = new JsonResponse($result, 200, array());
$response->setCallback($callback);
return $response;
The JsonResponse's constructor doesn't take the callback argument. You need to set it via a method call:
$response = new JsonResponse($result);
$response->setCallback($callback);
return $response;