How to call the JSON data from an url provided? - json

guys. I'm not familiar with API call. In this case, I would like to get the JSON data from an url provided by the client.
For example, the url could be https://test/test/test/test
and there are headers:
- partner_key: r935pokA8-98A1-r7y6-i90b-3940632d2q99
- regexkey: .*\.test\.ml.*
How should I get all the JSON data/strings stored in that url? Can I type in
https://test/test/test/test?partner_key=r935pokA8-98A1-r7y6-i90b-3940632d2q99&regexkey: .*\.test\.ml.* to get the data?
I tried the above method and it didn't seem to work...Since I'm quite newbie on this, not sure if I should write some code to get the data?
Thanks and I'm looking forward to hearing some answers!

Use postmen tool to get the data you can call the url like this,
and get a JSON data response

You can use jQuery this way:
$.ajax({
dataType: "json",
url: 'https://test/test/test/test',
success: function(data) {
// then use data returned JSON content...
}
});

Related

json file images won't load in ionic page

I am trying to load some images from a file on a server to display on a list. I get 404 not found error
after asking in forums, I get that the request URL is wrong, it looks inside the localhost not inside the json file.
https://filehost.net/db54d37849f75ddd
in the code I have a service which returns a response. I use that response in a controller and to the view. I get other information but no images
if anybody has a solution that would be really great.
please attach also some code, in this way it is easier to help you. About the problem, do have correct form of $http GET request? I mean does your get request points to correct address on server? something like this
`
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
`
you can check more here Angular $http

Problems with JSON getting data from tmi.twitch.tv api

Problem is when i try to use this code to get the log from tmi.twitch.tv api using url: http://tmi.twitch.tv/hosts?include_logins=1&target=70219146 i get Systax Error Unexpected Token. The code is:
$(document).ready(function() {
$.getJSON("http://tmi.twitch.tv/hosts?include_logins=1&target=70219146&callback=?", function (data) {
console.log(data.hosts)
});
})
I can get the data using php and json array like this:
$json_array = json_decode(file_get_contents('http://tmi.twitch.tv/hosts?include_logins=1&target=70219146'), true);
echo $json_array['hosts']['0']['host_login']."</br>";
But isnt there a way to use do this in html? thanks
You are trying to request regular JSON as JSONp (&callback=? activates jQuery's JSONp request method) which actually embeds the response into a <script> to execute it. However, the twitch API still returns JSON which is not valid JavaScript. Unless there is a way to make tmi.twitch.tv return valid JSONp, there is no way to do this directly from JavaScript unless you use a proxy like http://crossorigin.me/.

pass ajax response (json)data in php

So, I am stuck here and feel like banging my head.
My problem is I query a database by ajax and returned its response in json form. Now I have this response in the form of javascript variable but I want to pass it to a php variable for further use.
How can I do that ??
function getJson(url){
return JSON.parse($.ajax({
type:'GET',
url: url,
dataType:'json',
global:false,
async:false,
success: function(data){
return data;
}
}).responseText);
}
Here how do I pass data to php ??
FIrstly I gave you -1 for not learning the basics!
- You can send client values to server (php) as querystring
example: /server_link?json=JSON_VAL
(not recommended for whole json string)
- Send it as post body (look for jquery ajax post) and parse it with php json library.
- if you have just single level json ({name:'John', sname:'Smith)} you can send it to php server(as mentioned before ajax post) and read as request variable in php ($_POST, $_...).
As I never programmed in php you'll have to do it you're self.
A again: LEARN THE BASICS (client side, server side, post, get,...).

WebAPI Model Binding from JSON

I am creating an application using Durandal, with WebAPI as the server. I have a KendoUI grid that displays the data from the server correctly and functions properly until the POST or PUT methods are called. Here is my GET method:
and you can see that that data binds to the UI (used the data-bind extensibility in Durandal to change to kendo bindings):
Then I edit the data in the Grid and it passes the changes inside the request to the server as you can see in this Fiddler result:
On the server side I cannot get the data that is passed from the client to bind to anything I place as a parameter for the method on the POST or PUT.
I realize this is a couple different technologies to troubleshoot (e.g. Durandal, KnockoutJs, Kendo DataBinding, and WebAPI) but I think the fundamentals are working, the data is retrieved and bound to the UI and it is posted back when changed, but the WebAPI endpoint cannot bind to the data.
How can I get the passed "models" array to bind through the ModelBinding structure in WebAPI?
UPDATE- Here is the helpful JSFiddle that gave me the correct Content-Type to add: http://jsfiddle.net/Xhrrj/1/
new kendo.data.DataSource({
transport: {
read: {
type: "POST",
url: "../cccs/service.svc/SupplierSearch",
contentType: "application/json; charset=utf-8",
dataType: 'json'...
this is coming from the Telerik forum here
It looks as if it was mixing up form-urlencoded with json format - if you lookat the decoded string it is sending models= and then urlencoded JSON objects follow.
From my experience I think that your PUT end point declaration needs to look like this:
[HttpPut]
public void Put([FromBody]IEnumerable<Product> models) { }
So you need the FromBody attribute because your product array is in the body (I think?) and not in the url of the request.

Sinatra, JavaScript Cross-Domain Requests JSON

I run a REST-API build on top of Sinatra.
Now I want to write a jQuery Script that fetches data from the API.
Sinatra is told to response with JSON
before do
content_type :json
end
A simple Route looks like
get '/posts' do
Post.find.to_json
end
My jQuery script is a simple ajax-call
$.ajax({
type: 'get',
url: 'http://api.com/posts',
dataType: 'json',
success: function(data) {
// do something
}
})
Actually everything works fine as long as both runs on the same IP, API and requesting JS.
I already tried to play around with JSONP for Rack without any positive results, though. Probably I just need a hint how to proceed.
Use JSONP (JSON with padding). There is a JSONP extension for Rack.
Basically, you'll call:
$.ajax({
type: 'get',
url: 'http://api.com/posts',
dataType: 'jsonp',
success: function(data) {
// do something
}
})
which translates to a request like:
http://api.com/posts?callback=someJSFunc
and your server will respond, e.g.:
someJSFunc({"json":"obj"});
Of course, clients can do JSONP requests without jQuery. The trick with JSONP is you serve scripts, which can be cross-domain, rather than pure JSON, with cannot.
Thank's for the answers so far.
You were right and jsonp would solve the problem. The code snippets for javascript work fine.
To set up Sinatra is very easy as it is build on top of Rack.
Therefore simply install the rack-contrib gem
gem install rack-rack-contrib --source=http://gems.github.com/
(or put it in your Gemfile) and add
require 'rack/contrib/jsonp'
use Rack::JSONP
to your application.
This middleware provides regular JSON to non-JSONP clients and JSONP to jQuery & co.
It might be interesting to you http://github.com/shtirlic/sinatra-jsonp — this extension adds missing functionality to sinatra
Also available as gem gem install sinatra-jsonp
Try to call
$.getJSON("http://example.com/?callback=?",function(data) { alert(data); });
In this sample main keyword is construction "callback=?", so you need to process this param in your server-side script, and make a valid JSONP, like this:
function({ "foo" : "bar" });
Where "function" is random data, which is generated by jQuery automatically. Read more here about jQuery and cross-domain JSONP.