I have been trying to extract the information from this file for the past couple of hours and have run out of options.
http://www.nationalbanken.dk/dndk/valuta.nsf/valuta-hist.xml
Currently I have the information stored as a string in a Java servlet.
When I try to send it as a response to an AJAX request, I can't seem to extract any of the attributes through jQuery (mainly the time, currency, etc.)
I don't have a whole lot of experience with XML, actually none, so after trying various methods here on StackOverflow I have come to the conclusion that I simply can't find anything that'll work for me.
$("#btn").click(function() {
$.ajax({
url: "AjaxController?command=currency",
cache: false,
dataType: "xml",
success: processXML
});
});
This is my AJAX call to the servlet, in return it gives back the file linked above loaded into a string, which then needs to be processed and extracted by the jQuery function "processXML".
Related
I have tried the following line in my ajax call as it was suggested all over internet:
contentType: "application/json; charset=ISO-8859-1"
but it did not change anything. Whenever I make a call from ajax with a string parameter which has an accent for example società , at my controller(I'm using Java, spring framework), i receive it as SocietÃ. I have tried first UTF-8 which is the default (as far as I know) but nothing. Here is my full function:
function foo(customer, society) {
$.ajax({
url: 'notMappedAcoStaffing.do',
type: "GET",
contentType: "application/json; charset=ISO-8859-1",
data: { customer: customer, society: society},
success: function(data) { $('#content').html(data); },
error: function(error) { alert("error" + error); }
});
}
And I take the values on my backend as
#RequestMapping(method = RequestMethod.GET)
public ModelAndView fooController(#RequestParam("customer") final String customerParam, #RequestParam(value="society",required=false) Integer societyParam) {...}
Is there a way to resolve this issue?
Thanks for your help in advance.
UPDATE: I have still no idea why I receive it distorted as parameter at my controller. But however when I convert it as follows: How do I convert between ISO-8859-1 and UTF-8 in Java? ,it comes out all good. Seeing that it works, I tried it again to set UTF-8 in my ajax call but no success... It resolved my issue but honestly I do not like this way. Please share with me if you have a better solution maybe on the ajax call side. Thanks
Part of your problem is that you're using HTTP GET, which basically means that your data will be embedded in the URL itself, in the query string ( http://example.org/?customer=foo&society=bar ). This can lead to problems when using special characters.
Especially, the contentType property concerns the body of the request which you do not have at all so currently it has little impact.
Consider sending the data with HTTP POST (and remove your custom contentType) which gives you more power over the encoding of your data - which is then sent in the request body instead of the URL. It might solve your problem outright, or you might need to configure your server to use the same encoding as the client
- I suggest UTF-8 for both parties.
I am using Jquery Ajax calls to access REStful webservices as below. The webservice is hosted on different domain.
$.ajax({
type: "GET",
url: "some url hosted on differnt domain",
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(responseJson) {
alert("json"+responseJson);
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
Am not sure whether it is not hitting the webservice.It is going to error block but no alert is displayed. Am i doing anything wrong here?
Thanks!
You got to change the Header at you server side http://www.w3.org/TR/cors/#access-control-allow-origin-response-header
Since it will cause forgery. For more of cors understanding http://enable-cors.org/server.html
You can't make cross domain request just like that. It might cause cross-site forgery. Check this-
http://en.wikipedia.org/wiki/Cross-site_request_forgery
Basically, to be able to do so, the server should allow you to do that. If you access some resource on a cross domain server. The server responds back with a list of allowed users for that resource. The browser reads that list. If you are not listed in that list then the browser won't show the resource to you.
Solutions:
1- You should have control of the server side.
2-you can try 'script' tags in HTML for your purpose. They are an exception to this. You can make cross-domain requests using 'script' tags and then parse the response as json.
3-Jsonp callbacks.
I haven't actually implemented in much detail because I have control over server side whenever I need. And, CDNs are open to all. So, you might want to do some reading now to figure out more.
I'm in need of some desperate help. I've been at this for 4 hours, and I'm getting pretty worn out. :/ Here's my situation:
I have a Javascript application that is making a POST request (using jQuery $.post) to an external site. On the external site I have Apache Camel running with Jetty to expose it to the web. The web services I wrote in Camel expect JSON data for all of the requests. For instance, one request needs an id, so I send it {"id": 10}.
Here's my issue: it doesn't work from Javascript. I have a few different tools that will send post requests for me (like the Poster extension for browsers). If I use Poster and set the body to {"id": 10}, it works just fine. I get that exact string in the service.
But, if I post from Javascript, I get something different. Posting the JSON object will give me the string "id=10" on my service side. (It's OK for this scenario, but I will need actual JSON objects eventually.) If I stringify the JSON object, I get the JSON string, only all of the characters are escaped. (Ex. "%7Bid%33...").
I swear I've tried every method possible for posting the data, but I either get the weird already parsed JSON, or the escaped string (or nothing at all). Is there some way I can have Javascript NOT parse the JSON object and just send it (like my posting tool does)? If not, is there a safe, efficient way to un-escape the JSON string that I get?
I really appreciate any help.
I feel like we need a little bit more information, but take a look at this javascript plugin. It may be your solution: https://github.com/flowersinthesand/jquery-stringifyJSON
Try using jQuery.ajax and setting processData to false (defaults to true):
$.ajax({
url: '/where/to/post',
type: 'POST',
data: {"id": 10},
processData: false
});
Usually, jQuery converts anything in data to query string format like id=10. The processData flag tells jQuery to interpret it literally as a json hashmap.
Posting the JSON object will give me the string "id=10" on my service side.
Javascript does not do your this conversion, so your server does it.
It is likely that your server reacts differently based on the content-type of your POST e.g. application/json vs text/plain or text/html, a common feature of REST based services.
The answers here gave me a few hints, but ultimately, it was a lot of tweaking before it would work correctly. I had to do 3 things:
Add processData: false.
Turn the JSON object into a JSON string. The request wouldn't fire if I left it as an object (even if I changed contentType to application/json).
Change the contentType to text/plain. This sent it as a raw string.
And that's what did the trick. I now get the JSON string I want on the server side.
hi I am querying database to load all items based on some criteria and setting this result in session as
data = service.getData();
session.setAttribute("data", data);
now I am trying to access this data via an Ajax call and my Ajax call is served by a different servlet rather then which fetched the data from DB.
Ajax call using jquery
$.ajax({
type: "POST",
url: "/com/tp/AjaxXML.jsp",
data: ({cmd: "report"}),
dataType: "xml",
success: function(xml) {
$(xml).find('site').each(function(){
var url = $(this).attr('url');
});
}
});
in my AjaxXML.jsp
I am doing
if("report".equals(cmd)){
List<Object> data = (List<Object>)request.getSession().getAttribute("data");
if(data == null){
System.out.println("data is null ");
}
}
every time I am getting the data as null via the Ajax call how ever I try to access the session data normally from my first servlet it works.
could someone let me know if I am doing something wrong?
I noticed one more thing when we do session.getId(); and pageContext.getSession().getId();
both of them are returning different Id's? Is this expected to me they should be same anyone differ on that?
I noticed one more thing when we do session.getId(); and pageContext.getSession().getId(); both of them are returning different Id's? Is this expected to me they should be same anyone differ on that?
No, they should definitely not differ. I however assume that you have examined them within the same request. If you examined them in different requests, then the difference can be explained by the absence of the proper JSESSIONID cookie. Cookies are domain and context specific. You're apparently sending the ajax request to a different domain/context. The leading slash / in the ajax URL also confirms this less or more. Make sure that you're sending it to the same domain/context. Use a context-relative URL, something like as url: "AjaxXML.jsp" and move the code to the same domain/context, or turn on session sharing between different contexts at server level.
Unrelated to the concrete problem, doing the Ajax response handling in a JSP is a bad idea. Rather do it in a servlet.
Replace
url: "/com/tp/AjaxXML.jsp",
by
url: "/com/tp/AjaxXML",
and put the code in doPost() method of the servlet which is mapped on that URL pattern.
See also:
How to use Servlets and Ajax?
I'm porting working PHP application to Java/Wicket.
I have a lot of complex, well written jQuery/javaScript which I would like to reuse and not change too much.
Obviously I have to change urls in ajax calls and rewrite the server side scripts from PHP to Java.
I tought this task would be simple but somehow I can't figure out how to write server side that would respond to ajax call.
Simple example:
javascript:
function f(){
jQuery.ajax({
data: 'object_type=1&object_id=2',
url: 'ajax/get_object.php',
timeout: 2000,
type: 'POST',
dataType: 'json',
success: function(r) {
alert(r);
}
});
}
Php file ajax/get_object.php:
// ... create $json_string here
echo $json_string;
I have found AbstractDefaultAjaxBehavior which I probably should use to implement server side of such ajax call, but I'm not really sure how to use it.
I'm not really Java kind of guy so try to explain step by step what sould I do :-)
Have a look at This Ajax Wicket tutorial and search for AjaxEventBehavior.
Do note that Wicket assumes that browsers lacking javascript (Braille readers for the disabled for instance) can return full pages (full page reload in stead of AJAX). If you're doing a job for the government that's usually also a requirement.