How to Make a JSON Call to an Url

How to make a JSON call to an URL?

It seems they offer a js option for the format parameter, which will return JSONP. You can retrieve JSONP like so:

function getJSONP(url, success) {

var ud = '_' + +new Date,
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;

window[ud] = function(data) {
head.removeChild(script);
success && success(data);
};

script.src = url.replace('callback=?', 'callback=' + ud);
head.appendChild(script);

}

getJSONP('http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=js&callback=?', function(data){
console.log(data);
});

How to send json data in post-request to url in javascript

Axios can be used to send HTTP requests like the fetch api. For installing axios use: npm install axios (assuming nodejs is installed)

OR

you can use the CDN: <script src="https://unpkg.com/axios/dist/axios.min.js"></script> (paste this in your index.html)

axios({
method: 'post',
url: '/login',
data: {
update_id: 121631120,
message: {
message_id: 1,
from: {like so...}
}
}
})
.then((response) => {
console.log(response);
}, (error) => {
console.log(error);
}

For more details : https://blog.logrocket.com/how-to-make-http-requests-like-a-pro-with-axios/

JSON call with url as parameter

If homepage is an URL with http:// in it you need encode it.

You should write :

$.getJSON(url + "/addPerson/" + name + "/" + encodeURIComponent(homepage), function(data){console.log(data);} );

If that is the case, you should not pass urls in urls. Use the POST payload or multiform data.

Also, if the aim of the request is to add a record in your database, use POST instead of GET.

$.post(url+'/addPerson', {name: name, homepage: homepage}, function(data){console.log(data);});

How to pass a JSON array as a parameter in URL

I would suggest to pass the JSON data in the body as a POST request.But if you still want to pass this as a parameter in URL,you will have to encode your URL like below just for example:-

for ex json is :->{"name":"ABC","id":"1"}

testurl:80/service?data=%7B%22name%22%3A%22ABC%22%2C%22id%22%3A%221%22%7D

for more information on URL encoding refer below

https://en.wikipedia.org/wiki/Percent-encoding

Sending a JSON to server and retrieving a JSON in return, without JQuery

Sending and receiving data in JSON format using POST method

// Sending and receiving data in JSON format using POST method
//
var xhr = new XMLHttpRequest();
var url = "url";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
console.log(json.email + ", " + json.password);
}
};
var data = JSON.stringify({"email": "hey@mail.com", "password": "101010"});
xhr.send(data);

Sending and receiving data in JSON format using GET method

// Sending a receiving data in JSON format using GET method
//
var xhr = new XMLHttpRequest();
var url = "url?data=" + encodeURIComponent(JSON.stringify({"email": "hey@mail.com", "password": "101010"}));
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
console.log(json.email + ", " + json.password);
}
};
xhr.send();

Handling data in JSON format on the server-side using PHP

<?php
// Handling data in JSON format on the server-side using PHP
//
header("Content-Type: application/json");
// build a PHP variable from JSON sent using POST method
$v = json_decode(stripslashes(file_get_contents("php://input")));
// build a PHP variable from JSON sent using GET method
$v = json_decode(stripslashes($_GET["data"]));
// encode the PHP variable to JSON and send it back on client-side
echo json_encode($v);
?>

The limit of the length of an HTTP Get request is dependent on both the server and the client (browser) used, from 2kB - 8kB. The server should return 414 (Request-URI Too Long) status if an URI is longer than the server can handle.

Note Someone said that I could use state names instead of state values; in other words I could use xhr.readyState === xhr.DONE instead of xhr.readyState === 4 The problem is that Internet Explorer uses different state names so it's better to use state values.

Call a servlet to write data in JSON format to a URL in the server

You should set the name of jsonCreates.json same as java servlet Name (jsonCreate)

   @WebServlet("/jsonCreate")

The reason why you dont get the json data is because response.sendRedirect() does not forward any data, it just makes a redirection(navigation) to a page.

If you want your JSON data to be accessible throughout your page navigations then you should create a session and set a session variable to hold this json data.

JAVA

HttpSession session = request.getSession(false);
session.setAttribute("variable", "json value");
response.sendRedirect("/page");

JSP

<%
out.println(session.getAttribute("variable"));
%>

OR

you can use forward() as below:

JAVA

request.setAttribute("variable", "JSON data");
RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);

JSP

<%
out.println(request.getAttribute("variable"));
%>

How to request JSON that have spaces in URL?

Escape your url.
Should be like this: http://api.wunderground.com/api/81eb946ab954bdcb/hourly/lang/q/Canada/Bachelors%20Island%20Marine.json

Supposing you have a String as your url , you could do:

 url.replaceAll(" " ,"%20");

EDIT

As many suggested, the most correct approach would be:

 url = URLEncoder.encode(url, "UTF-8");

To avoid any problems like these in the future.



Related Topics



Leave a reply



Submit