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..
Related
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.
I'm working on a Java Jersey REST API and a website using this API.
This is my server side method
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<TreeViewModel> getTreeList() {
User user = User.getByCredentials("team", "team");
List<TreeViewModel> list = user.getTreeViewModels();
return list;
}
And this is my javascript request
function requestTrees() {
$.ajax({
type: "GET",
url: window.api + "tree",
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", make_base_auth('team', 'team'));
},
success: function(data) {
console.log('suc');
},
complete: function(jqxhr, txt_status) {
console.log('com');
},
failed: function(data) {
console.log('fai');
}
});
}
If I debug my server the method gets called and returns the list. But at client side I receive a 500 error which indicates something is wrong at server side?
Any suggestions?
Thanks in advance.
500 means internal server error, and should usually be accompanied by log messages or even in some cases an exception embedded in the message body sent to the client.
What has most likely happened is that your method has processed and returned the List<TreeViewModel> but that could not then be serialized for sending.
For example some serializers do not like working with raw collections. You need to have your own object and then put the list within that. The only way to find the actual cause of the problem though is to find that exception and see what it is telling you.
I am calling server side method using jQuery AJAX and I am sending json string to controller .
When the json size is small it is working well but when json size increases (say more 7.kb) it is not accepting json string from server side.
I think there is limitation in return size in jQuery AJAX method, can anybody help me to overcome this problem or suggest any alternate option to to send large js
My client side code is like,
$.ajax({
url : 'savingurl.json',
method : 'post',
data : {
jsonStr : jsonStr
},
success : function(data){
alert("success");
}
});
In above my success function is not getting executed when there is more data in jsonStr but when there is less data, it is executing fine.
Thanks in advance.
Thank u all .. now its working fine . The problem was with ajax method ,
instead of method: in ajax put type: ,then it will take large data ..
$.ajax({
url : 'savingurl.json',
// method : 'post',
type : 'POST',
data : {
jsonStr : jsonStr
},
success : function(data){
alert("success");
}
});
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 am new to jQuery and am having trouble following the documentation. I have a variable in JavaScript (that I already obtained from an html form) that I would like to send using AJAX to the server. I want to the server to do whatever it needs to do with that value and then reply with either "success" or "failure" (I know how to do the backend part). How do I send the request and then receive the reply with jQuery?
To send the request you use the $.post or $.get function (or the $.ajax function if you want loads of control)
$.post('example.com/script', {
'param1':somaVar
}, function(data){
alert(data); //data is whatever the server returned
});
For both $.get and $.post the argument order is like this: (url, [data], [callback], [type]). Url is the url to which the request is done, data is the data with which the request is send, callback is a function that is executed when the request is complete. Type is the type of data the server will return (jQuery usually finds this out on itself) possibles are "xml", "html", "script", "json", "jsonp", or "text"