Getting Temperature and Humidity from NOAA web api v2 - java

The new NOAA api says that I need to put a token in the header for me to send request and it is giving me a token required error without it. I entered my email and received a token, but I am unsure on how to put it in a format that I can get a response.
Is there a way I can get a JSON response by posting all the information in the URL or do I need to make a html/php page? If I do need to create a web page, is there a library I can import that will allow me to get the JSON in java without the need for a webpage?

you do not need to import any library for accessing NOAA-API you can directly call it using token.
you will have to add your token in header if you are calling it through AJAX call.
open:- http://js.do/
1.add a script
Run this code using your token value.
<script>
function testjson(){
//alert("inside testjson");
jsontest = $.ajax({
type: 'GET',
url: 'https://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCND&locationid=ZIP:28801&startdate=2010-05-01&enddate=2010-05-01',
//you can use different data-set values.
headers: {
Token: 'provide your token here'//example:'kxhfoJOtnEuxSNnMGMMSEITkmcsAFmFT'
},
// async: false,
dataType: 'json',
success: function (data) {
//Do stuff with the JSON data
alert(JSON.stringify(data));
jsontest = data;
console.log("data is: " + data);
},failure: function(){
alert("ajax failed");
}
});
console.log(jsontest);
//console.log(jsontest[0]);
}
testjson();
</script>

Related

How to call an AXIS web service via AJAX?

I'm very new to web services. I used eclipse and some tutorials from the web to create a simple web service called DeScriptor which I uploaded to a Tomcat server. It's accessible through this URL
http://www.xwizard.de:8080/services/DeScriptor
and according to the message written out there, it appears to be working (right?).
So far, so good, but now I don't know how to call it. The service has a single method String retrieveSVGFromScript(String scrp) which I tried to call with this AJAX code:
var hallowelt = "Hallo Welt";
var params = JSON.stringify({scrp: hallowelt});
$.ajax({
type: "POST",
url: "http://www.xwizard.de:8080/services/DeScriptor/retrieveSVGFromScript",
data: params,
dataType: "json",
contentType: "application/json; charset=utf-8",
crossDomain: true,
success: function (msg) {
console.log(msg.d);
},
error: function (xhr, status, error) {
// Some error handling.
}
});
hoping that I'd get the result string of the method by msg.d, but instead I got this not so informative error message:
jquery.js:8630 POST http://www.xwizard.de:8080/services/DeScriptor/retrieveSVGFromScript 500 (Internal Server Error)
Can somebody point me in the right direction?
EDIT: You can find the WSDL here: http://www.xwizard.de:8080/services/DeScriptor?wsdl
You are trying to call a server webservice using REST style (i.e. setting content-type, providing params as JSON message, etc.).
But the webservice expects a SOAP message. An example how to send a SOAP message with Javascript can be found here.

Calling web service through jQuery and passing object to rest web service which accepts java object

My web service is working fine when I am testing it through rest client
but when I try to call it through jQuery it fails . the main problem is basically with serialization of JSON data into the object.
My web service is like this
$.ajax({
type: "POST",
data: JSON.stringify({"userId":124,"emailId":"ranjeet#triconinfotec.com","role":"instrutor","date":"2014-08-01","target":"Section",
"sectionId":234,"sectionName":"Economics","assignmentId":9991,"assignmentName":"EZT","isbn":"124XSD234","courseId":33,
"courseName":"GeneralEconomics","ipaddress":"192.168.1.210","pageId":"sd345"}),
url: "http://localhost:7001/connect/restservices/insight/assignmentgraph/connecttrack/activity",
contentType: 'application/json',
dataType: 'json',
success: function(){
// we have the response
alert("Success");
},
error: function(e){
alert('Error: ' + e);
}
This web service get called but its goes to error and display error [object object].
I think the problem is with serialization date in the content because in my Java file this class date has a date type instead of string.
Change the error handler as below, and then investigate the response:
error: function(x, e) {
alert('Status code: ' + x.status + ', Error: ' + e);
}
Your web service needs to know how to convert a String to a Date. You didn't mention what framework you're using on the server to create your web service, but I imagine it has the ability to setup a converter. If not, a quick and dirty work around would be to change the type of your field to String, and then once the request from the client comes in, use something like SimpleDateFormat to parse the String into a Date and store it in another field.

JQuery was not Called

I am getting errors on callBacks. I have tried following code in jsfiddle.com . You can also try. Data from servelet is not returning. It's returning same error again and again. Check jquery library when you try in jsfiddle
$.ajax({
url : 'http://192.168.16.111:8081/MiddleWareUsman/androidServlet',
type : "post",
dataType: "jsonp",
data : {
"fname": "chaaaaapiio",
"lname": "gya"
},
success : function(data) {
alert("hello"+data);
},
error : function(xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});
My server side:
String a=request.getParameter("fname");
String b=request.getParameter("lname");
response.getWriter().write(a+" "+ b);
It appears you have a couple problems.
JSONP requests can't be sent via POST. They are actually sent as <script> tag requests anyway which are GET requests.
Your server isn't doing JSONP. For the server to do JSONP, it must wrap the requested data in a call to a javascript function who's name was passed as an argument to the request and then the actual data is passed as an argument to that function. JSONP is a big hack, but it works by requesting a javascript and that's what the server must return.
you simply can't send a POST request using JSONP
check this link out to see how JSONP works..

Jquery ajax call returns error when the rest service is returning a string

Trying to send a simple text from my rest service and read it using ajax call. Found many answers about jsonp and cross browser compatibility, tried crossdomain too.
Here is the rest service:
Trimmed everything down to send only a simple string.
#GET
#Path("/getcontents2")
#Produces({MediaType.TEXT_PLAIN})
public String getContents2(#QueryParam("name") String msg) {
return "abc";
}
The ajax call:
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'http://metrics/getcontents2?name=Work/loc.txt',
crossDomain: true,
async: false,
dataType:'html',
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
console.log(xhr.responseText);
console.log(xhr);
},
});
});
The browsers opens up the string as is. I guess something is really wrong in the jquery script.
Error on Firebug:
GET http://metrics/getcontents2?name=Work/loc.txt 200 OK 4ms
0
(an empty string)
(an empty string)
Object { readyState=0, status=0, statusText="error"}
Fixed it!
It was because my server was not supporting cross-domain. Configured it will corsfilter and it worked like a charm!
try setting your datatype to text jsonp
dataType: 'jsonp',
http://api.jquery.com/jQuery.ajax/
You will have to do one of two things to go with this to resolve the error further;
making changes on the server side to pass the data back as json and not as text
retun the string encoded in json return "{"text":"abc"}"; //or something like this
Why?
jquery cross-domain requests are only allowed for dataTypes "script" and "jsonp".
I have updated your fiddle it still throws an error but that is related to a parse json error

I'm unable to receive the json object sent from my servlet

$(document).ready(function() {
var path = null;
console.log('${pageContext.request.contextPath}/loadfile');
$.ajax({
dataType: "json",
url: '${pageContext.request.contextPath}/loadfile',
success: function(data){
$.each(data,function(index,obj){
console.log(obj.id);
alert('inside');
path = obj.path;
});
}
});
here /loadfile is the url which returns the json object , when I go to this url I am able to see the JSON object printed on the html page , however I dont get the same when I access the page which contains the above javascript code
Often people don't tell their server to the the browser that the JSON string they are sending is to be interpreted as a json object.
Despite the fact that dataType:'json' is supposed to sort it out, it is not always the case.
in PHP
header("Content-type: application/json");
ASP
Response.AddHeader('Content-Type', 'application/json');
Failing that,
success: function(data){
if (typeof data!='object') data=$.parseJSON(data); // make sure it's an object
I can't figure out why jQuery doesn't fix it but the response headers, even with dataType:'json' set can appear as application/x-www-form-urlencoded; charset=UTF-8 and the object doesn't get created.

Categories