how to use $.ajax - json

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.

Related

How to insert bulk (20,000) json data post in ajax jquery

I need Bulk (20,000 Records) json data to post Controller.
I Get This Type Of Error
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. Parameter name: input
$.ajax({
type: 'POST',
url: baseurl + 'Masters/AuthMapping/MultiBpsave',
dataType: "json",
data: JSON.stringify(BPR[0]),
contentType: "application/json; charset=utf-8",
async: false,
success: function (data) {
},});

how to return json from groovy please?

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()

creating an array and sending to ajax

I'm trying to gather information from all of the textareas on my page, and then put that information into an array so I can send to the server via ajax/json.
Although I am not quite sure how to go about doing it.
I'm not sure how to pull the information I need from the
Here is what I have so far:
Example of my HTML
"<textarea id='tbObjective_" + counter + "' name='txtObjective' class='objectives' sequence='" + counter + "'></textarea>"
jQuery:
var objectiveList = [];
$('.objectives').each(function (objective) {
objectiveList.push({
id: objective.id,
sequence: objective.sequence,
text: objective.val()
});
});
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: objectiveList
});
Any help would be appreciated
Thanks
you can proceed with the following procedure. i have used python django and html5 for the purpose
1. make a text box which is hidden and after generating your json document set it in this text box (suppose id of textbox is "submit_json") than use
$("#submit_json").val(JSON.stringify(formData, null, '\t')), // (formData, null, '\t') this is js function that i have written for the ourpose
data = JSON.stringify({"jsonDoc":Generated JSON})
console.log(data);
$.ajax({
url: 'http://127.0.0.1:8000/catchjson/',
type: 'POST',
async: false,
contentType: 'application/json',
data: data,
dataType: 'json',
processData: false,
success: function(data){
alert('Done')
//Goto Next Page
},
error: function(jqXHR, textStatus, errorThrown){
alert("Some Error!")
}
})
Now on server you can catch this json if u have problem creating json from your text box let me know
I often use:
var formData = $("form").serialize();
for posting data over ajax, so maybe you could try:
var textareaData = $(".objectives").serialize();
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: textareaData
});
or alternatively make sure all the fields are in a form element and use the first example.
You can parameterise it with $.param(objectiveList)
see jQuery.param()

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);}

ajax post multiple parameters does not pass properly to asp.net mvc action

I am not able to pass 2 parameters to the action method as json. I have the following ASP.net MVC3 action.
[HttpPost]
public JsonResult Create( PatientContactEpisodes patientcontactepisode, IList<PatientContactEpisodeProcedures> patientcontactepisodeprocedures)
And my ajax post looks like this.
$("#SaveButton")
.button()
.click(function () {
var episode = getpatientcontactepisode();
$.ajax({
type: 'POST',
traditional: true,
url: "/User/PatientContactEpisodes/Create",
contentType: 'application/json, charset=utf-8',
data: {patientcontactepisode: JSON.stringify(episode), patientcontactepisodeprocedures: JSON.stringify(createArray)},
//data: JSON.stringify(episode),
dataType: "json",
success: function (results) {
alert("success");
}
});
});
ISSUE
The issue is that the value does not seem to be passed when I send both parameters to the action. Both are null/empty. Whereas, when i send a single parameter
//data: JSON.stringify(episode), or //data: JSON.stringify(createarray),
The above works fine.
Try:
data: {JSON.stringify({patientcontactepisode: episode, patientcontactepisodeprocedures:createArray})},
This should do the stuff for you.