How to store my JSON data in Ck Editor - javascript

Here is my code and its not print or display my json data in ckeditor
function getRules(){
$.ajax({
url: "api",
type: "POST",
data: {
version:'0.1'
},
dataType: "json",
success:function(response){
console.log(response.data.recordslist.RulesDetails);
CKEDITOR.instances.txtEdit.setData(response.data.recordslist.RulesDetails);
},
error: function (xhr, ajaxOptions, thrownError) {
//swal("Error!", "Please try again", "error");
}
});
}

If Your URL is correct and you have URL by the name of api only edit success function to
success:function(response){ $('#txtEdit').text(response); }
this show a result in array you need to extract the array.
if you don't want to extract the array. remove
dataType: "json"
it work fine. and write the data in text editor.

Related

Url that have been passed to long

Im writing this code for save an url in a file txt, that i will use too pass after the same for invoke the button share.
my problem come when i use this code
$.ajax({
url: "/condividi.php",
dataType: "json",
data: {
res: osrm_result
},
success: function(data, textStatus, jqXHR) {
alert("ciaociao");
window.open("https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2F52.16.81.189%2Findex_gen.html&src=sdkpreparse");
}
});
this trigger the error: Url too long
how i can correct this to make it work
the default method which jQuery uses is GET which has limited data length,
try sending the data by POST (you should of course update the condividi.php accordingly to serve the POST request):
$.ajax({
type: "POST",
url: "/condividi.php",
dataType: "json",
data: {
res: osrm_result
},
success: function(data, textStatus, jqXHR) {
alert("ciaociao");
window.open("https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2F52.16.81.189%2Findex_gen.html&src=sdkpreparse");
}
});

Show Ajax error response as a webpage

from an Ajax call that was build up like this:
function GetData() {
$.ajax({
type: "POST",
url: "#Model.RouteForAjaxLastValue",//"/RCharts/AjaxMethod",//
data: "{Id: " + lastID+ "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
})
I sometimes get responses like this:
I can see that this is meant to be displayed as a webpage since its an entire page html formatted.
But is there a way to display that directly as a new page mabye?
It cuts of the rest of the message in the alert before I can even get to read what the error may be...
Use $("html").html(response.responseText); inside your success function .
success : function(response){
$("html").html(response.responseText);
}
You should probably use console.log()
but this should append it to the body:
$('body').append(response.responseText);

transfer selected table's row (javascript DataTables) to server

here is about a javascript widget DataTables. An example can be found here.
sorry, i am not a javascript specialist. How can i transfer the selected row (practically my objects from server) back to the server in form of json-format ?
i did try to do it with this approach, but it doesn't work:
$('#save_btn').click( function () {
//table.row('.selected').remove().draw( false );
console.log ( table.rows('.selected').data());
var stringData = table.rows('.selected').data().serialize();
$.ajax({
url: '${pageContext.request.contextPath}/ajax/storeSelectedContacts',
data: stringData ,
type: "POST",
cache: false,
success: function (savingStatus) {
alert("success");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("error")
}
});
} );
many thanks
First, its return array of objects.
var stringData = table.rows('.selected').data();
Second, for convert array to JSON ...
var aData = table.rows('.selected').data();
var sData = JSON.stringify(aData)
and for send to server you slould indicate that is JSON dataType: 'json'
$.ajax({
url: '${pageContext.request.contextPath}/ajax/storeSelectedContacts',
data: sData ,
type: "POST",
cache: false,
dataType: 'json',
success: function (savingStatus) {
alert("success");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("error")
}
});
} );

Jquery $.ajax not posting a string of data in ASP.NET MVC

I have a situation where I am building the json data to post dynamically. so I create it in a for loop and then attempt to use the $.ajax command, but on the server side the model is null. If I hard code the data section as would be normal, it works fine. I tried the same process with a simple model just to check and the same happens.
So this works:
$.ajax({
url: '#Url.Action("SetupQuestions", "Login")',
type: 'POST',
dataType: 'json',
cache: false,
data: {SharedSecret: 'Bob'},
success: function (data, status, xhr) {
$('#divMFAQuestion').html(data).fadeIn('slow');
},
error: function (xhr, status, error) {
alert("MFA Challenge Error (b): " + error);
}
});
But this doesn't:
var datastring = '{SharedSecret: Bob}';
$.ajax({
url: '#Url.Action("SetupQuestions", "Login")',
type: 'POST',
dataType: 'json',
cache: false,
processData: false,
data: JSON.stringify(datastring),
success: function (data, status, xhr) {
$('#divMFAQuestion').html(data).fadeIn('slow');
},
error: function (xhr, status, error) {
alert("MFA Challenge Error (b): " + error);
}
});
Nor this:
var datastring = 'SharedSecret: Bob';
Any thoughts?
You have quotes around your entire JSON data structure:
var datastring = '{SharedSecret: Bob}';
JSON.stringify expects a JSON structure, so the quotes should only be around the string part for JSON.stringify to work:
var datastring = {SharedSecret: 'Bob'};
However, the reason that your AJAX call is not working is that the data parameter accepts a JSON data structure. JSON.stringify will serialize it as a string, which data does not expect. So you need to just pass the fixed datastring without JSON.stringify.
data: datastring

Want to get base64 data from a file URL

I want to get base64 data and i have a url which will be give me the file.
I have tried the below mentioned way to get base64 data from URL
I have made a ajax call to the the given URL
$.ajax({
type: "GET",
url: DownloadUrl,
success: function (data, textStatus, jqXHR){
if (data){
}
},
error: function (xhr, statusText, errorThrown) {
console.log(statusText);
}
});
In the success event, i am getting some decoded data, I have encoded that using "window.btoa". But now my file is getting corrupted after this way.
Is this the right way to get base64 data?
Any other Way to get this data?
Did you try to call encodeURIComponent on the returned data?
$.ajax({
type: "GET",
url: DownloadUrl,
success: function (data, textStatus, jqXHR) {
if (data) {
data = btoa(encodeURIComponent(data));
}
},
error: function (xhr, statusText, errorThrown) {
console.log(statusText);
}
});

Categories