I'm trying to write a simple 'proof of concept' front end for our webservice. The webservice is a java webapp returning simple xml running in tomcat.
The front end consists of simple html pages with some jquery functions. I'm developing the pages on my local machine while the webservice runs on one of our servers.
Basically this is what I do:
var url = "http://ourserver.com:51088/service/action/?param=123";
$.get(url,function(data,status) {
alert("Data: " + data + "\nStatus: " + status);
});
When I put the url in the Firefox address bar, I get the resulting xml.
When I run the jquery code Firebug shows the resulting xml, but the alert never shows.
Thinking it might be a cross-browser scripting problem, I wrote a little node.js proxy server that passes any localhost:51088/path to ourserver.com:51088/path. So I changed the url var to
var url = "http://localhost:51088/service/action/?param=123";
Again, testing this url in the browser results in the xml. So the node.js proxy server is working fine.
When I run the jquery code in Firebug I now consistently get Reload the page to get source for: http://localhost:51088/.....
I'm at a loss now.
UPDATE: after reading more I changed the jquery code to:
$.ajax( {
type: "GET",
contentType: "application/xml",
url: url,
datatype: "text xml",
xhrFields: {
withCredentials: true
},
succes: function(xml) { alert(xml) },
error: function(obj, status, err) { alert ("error\nstatus: " + status + "\nerr: " + err)}
});
Now it doesn't matter if I use the remote url or the local url. Both return the xml in the console log, but the success function is still not called.
You misspelled the "success" option.
Seems to me your proxy is not working as you expect.
And as to your first problem, seems to me that it is cross-domain request issue.
Related
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 need to test the browser window width in JSP, to determine whether or not I have to set a value in the request object. Typically I would achieve this with something like this:
if(document.body.offsetWidth < xxxx){
// ...
}
But I don't know how to do it with a JSP expression.
You might misunderstand that jsp and javascript existed on same file. Yes but JSP part compiles on server side itself comes to client.
You can't do that with JSP.
JSP compiles on serverside Where as javascript plays on browser i.e on client side.
Do it with javascript.You should do it in document load
when you do if(document.body.offsetWidth < xxxx) you are doing it at client side. Now if you want to propagate some client side value to server side i.e to some java class(most of the cases it will be servlets) then you have two options:-
1)Make an AJAX call. (You can also use jquery here to make AJAX call).
Here is the small example, you can find ample on google
$.ajax({
type: 'GET',
dataType: 'json',
data: { windowWidth: "100"}
url: servlerURL,
success: function(reply) {
},
error: function (xhr, textStatus, errorThrown) {
}
});
or
2)Submit the form
Let's say I need to upload files to a server programmatically using uploading a file programmatically can a server whatever php java or asp.net return me a value when using POST request method instead of GET ?
If not how can I ask the server which files have been uploaded correctly or that uploads have finished ?
I mean if the server is ANOTHER server than mine I can't see how to get the response so can you give some sample code or refer to some urls.
It can, same way as GET requests e.g. server can answer with JSON format string response which will include uploaded files or it can return full html page, whatever you need.
E.g. if you want to send some data to server using POST method and you want to alert it you can do something like this (jQuery):
$.ajax({
type: "POST",
url: "http://localhost/testing.php",
data: { name: "John Doe" } // sample data sent to server
}).done(function( msg ) {
alert( "Response data: " + msg );
});
Generally it shouldn't matter if it was GET or POST, response is returned from server, the only thing you need to do is catch it and perform actions based on response content.
I wrote a server program in java but in order to give an interface with web i want to access java method in jsp when certain menu button is clicked. How can i do this?
Using ajax (using jQuery.ajax, you could make a request to server, In your case may be to a Servlet which will invoke method on server that you requested
For example:
function callMe(){
$.ajax({
type: "POST",
url: "/someServlet",
data: { methodToInvoke: "sayHello" , data: "Abc" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
}
at Servlet end
doPost(...){
String methodToCall = request.getParameter("methodToCall");
//do some stuff to determine method to call and call it like
methodService.invoke(request.getParameter("data"));
}
Also See
DWR
you cannot do this directly because JSP is server side and html is client side. However, it can be accomplished via AJAX. http://en.wikipedia.org/wiki/Ajax_(programming)
my web side html code like this
$.ajax({
type : "Get",
url : "http://localhost:9999",
data : {"servicename" :"test"},
timeout:100000,
beforeSend: function(xhr) {
},
success: function(rs) {
alert("[success]" + rs);
},
complete:function(XMLHttpRequest,textStatus){
if(XMLHttpRequest.readyState=="4"){
alert("response text= " + XMLHttpRequest.responseText);
}
},
error: function(XMLHttpRequest,textStatus,errorThrown){
alert("error:"+textStatus);
}
});
I Use url querystring (http://localhost:9999?servicename="test") can receive data ,but i use jquery reveive nothing.
jquery version is 1.4.
Netty Server is run example HttpSnoopServer.java.
After running this locally my guess is the same as devnull69. You can not make an XMLHttpRequest to localhost from another domain.
Cross-site XMLHttpRequest
Try executing the same javascript in chrome's console or in firebug's console, it should spit out an error about domain access.