how to return json from groovy please? - json

My environment is openjdk version "1.7.0_75", tomcat-7.0.82, groovy-2.4.13, jquery-3.2.1.
Now I want use ajax post some data to groovy, and want groovy return json to ajax.
Ajax:
$.ajax({
type: "post",
contentType: "application/json; charset=UTF-8",
url: "edit.groovy",
data: json,
dataType: 'json',
success : function(data) {
console.log('ok');
},
error: function(data) {
console.log('err');
}
});
And edit.groovy:
response.contentType = 'application/json';
out << "{rs: 2}";
My question is, why the log is 'err', is my groovy return a right json type please? How to fix it please?

"{rs: 2}"
Isn't valid json the name had to be in double quotes
'{"rs": 2}'
Just use a builder with a map, and it will be done for you
new JsonBuilder([rs: 2]).toString()

Related

JSON parsing from cross domain using jquery ajax

Im trying to parse json from cross domain but im getting error like 405 (Method Not Allowed) in jquery plugin (im using latest plugin only from google) Any solution or suggestions will be great help for me.
Thanks
Basha
Here is my code
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://myurl.com/webservice&callback=?",
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "jsonp",
data: "{}",
Accept: "",
beforeSend: setHeader,
success: OnGetAllMembersSuccess,
error: OnGetAllMembersError,
});
});
function setHeader(req) {
req.setRequestHeader("Authentication", "Basic credentials");
req.setRequestHeader("Content-Type", "application/json");
req.setRequestHeader("Accept", "application/json");
}
function OnGetAllMembersSuccess(data, status) {
alert(status);
$.each(data.result, function(key, value) {
$("#result").append(key+" : "+value);
$("#result").append("<br />");
});
}
function OnGetAllMembersError(request, status, error) {
alert(status);
}
While using jsonp as dataType, you need to bind a call back function in the server side..
for example, if you need a json response like {"id":"myId"}, at the server side it should be return like "mycallback({"id":"myId"})";
Also you need to write that function in client side too.
function mycallback(json)
{alert(json);}

Web service call from .ajax, maxJsonLength error

I'm using .asmx web service and web service method can serialize to json string(3506 record) by using JavaScriptSerializer class and i don't get any error from VisualStudio.
On the other hand my phonegap app using this service and i can consume with that. But my last method contents 3506 records and when i call this method from javascript i get
"error during serialization or deserialization using the json
javascriptserializer"
error.
Should i use paging or smth on web service? If yes can anybody tell me how to do that? Like i said i can use this method from browser. i can get this error when i call on javascript.
$.ajax({
type: "POST",
url: "WEB_SERVICE_URL",
data: "{ dummyParameter:dummy }",
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function() {
app.showLoading();
},
success: function(msg) {
clCard = JSON.parse(msg.d);
alert(clCard.length);
},
complete: function() {
app.hideLoading();
},
error: function(msg) {
app.hideLoading();
alert('Error');
}
});
Please have a look at this. I think helped you.

how to set jsonp callback in retrieving restful web service data via jquery

I have asked a question regarding invalid label on firebug which happens when I try to retrieve data from my restful web service. Most of the answers I received was about setting a callback function. but I can't seem to get the right juice from it. can you show me an exact code on how to do it? or atleast correct the code here:
type: "GET",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata:true,
jsonp: false, jsonpCallback: "success",
url: 'http://localhost:8732/Service1/data/10',
success : function success(data) {
jsonResponse = eval("(" + data + ")");
alert(jsonResponse)
},
error : function(req,status, ex) {
alert(ex);
}
Thanks,
Wow, you've got a lot of unnecessary stuff there. Try this:
$.ajax({
url: 'http://localhost:8732/Service1/data/10',
dataType: 'jsonp',
error: function(req, status, ex) {
// your code here
},
success: function(data) {
//your code here
// Please note: you don't need to 'eval' the json response.
// The 'data' variable will be loaded with the object that the json string represents.
// By the way, don't use 'eval', ever.
}
});
jQuery will take care of the callback on the url. See here: http://api.jquery.com/jQuery.ajax/
UPDATE
Why jsonp? If you're getting this from localhost, why not just json? As discussed in the comments below, your server currently is not capable of a jsonp response, but it can do json.

how to use $.ajax

when I navigate to
http://localhost:54763/Colorbox/Service.svc/GetCustomers,
I get the json data displayed..
but this on the client side did not generate the json data I need.. why?
$.ajax(
{
type: "POST",
url: "../Service.svc/GetCustomers",
dataType: "json",
data: {},
contentType: "application/json; charset=utf-8",
success: function (json) {
var output = json;
$('#MyTemplateOutput').html(output);
});
}
});
you should try using the full url like so and edit this line:
url: "http://localhost:54763/Colorbox/Service.svc/GetCustomers",
try that and see if you get a response back.

Not Receiving JSON object into my Zend Controller

Am am successfully parsing and sending JSON values from my client for my server side controller to receive and decode
$("#test2").click(function() {
$.ajax({
type: "POST",
url: "<?php echo $this->baseUrl() ?>/expensetypes/add",
data: JSON.stringify(wrapFormValues($('#expensetypes'))),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
However in my controller the code $this->getRequest()->getPost() doesn't seem to receive the JSON object that my client is sending though Firebug clearly shows that my JSON object is being parsed and sent.
What am I missing?
try
print_r($this->getRequest->getParams());
and see what that shows you