CKEDITOR load data from database AJAX - json

Hi guys I need to load data from my database into CKEditor. I searched it allready on google but I could not find recent solutions, and I tried some of them but they did not work. I tried this : var data = CKEDITOR.ajax.load( data.data[0].name );
this is my json
{"result":true,"message":"Post data success","data":[{"url":"sales","naam":"Sales\/B2B","id":"80","koppelid":"80","omschrijving":"
Sales\/B2B<\/p>\r\n"}]}
and here is my AJAX
$(document).on('focusout','#select-afdeling',function(){
$.ajax({
url: 'admin/afdeling/afdeling',
type: 'POST',
dataType: 'json',
data: $(' #select-afdeling').serialize(),
success: function(data){
if (data.result !== true) {
}else{
$("#afdeling-naam").val();
$("#afdeling-naam").attr("value",data.data[0].naam);
$("#afdeling-naam").val();
}
}
});
});

You can use SetData().
Example:
CKEDITOR.instances.editor1.setData('<p>This is the editor data.</p>');

Related

Problem with php handling ajax in the same file

I have a serious problem, I can't receive data sent by ajax in php. I've read many tutorial about that but it still not resolved. So if you guys have the magic solution, it'll make my day.
Here is the code, note that it is in the same file problem.php.
assocStored is an array or object, and it have the right data if I check it on jvascript
window.onload = function(e){
var assocStored = JSON.parse(localStorage.getItem("associes"));
$.ajax({
type : "POST",
data : {"problem" : assocStored},
success : function(res){
console.log("action performed successfully");
}
})
}
<div>
<h3>php</h3>
<?php
var_dump ($_POST);
if( isset($_POST['problem']) ){
foreach ($_POST['problem'] as $associe) {
echo($associe["sex"]." ".$associe["firstname"]." ".$associe["lastname"]);
}
exit;
}
?>
</div>
As my comment above, I guess your request send a GET method.
In your code, you are using type is POST but type is an alias for method. You should use type if you are using versions of jQuery prior to 1.9.0.
So you can modify your ajax to here:
$.ajax({
method: "POST",
data : { "problem" : JSON.stringify(assocStored) }, // convert to json
dataType: "json", // add type
success : function(res){
console.log("action performed successfully");
}
})
If it continues not working, add this code to ajax:
$.ajax({
method: "POST",
data : { "problem" : JSON.stringify(assocStored) }, // convert to json
dataType: "json", // add type
beforeSend: function(req) {
if (req && req.overrideMimeType) {
req.overrideMimeType("application/j-son;charset=UTF-8");
}
},
success : function(res){
console.log("action performed successfully");
}
})
I hope it works.

Posting object using ajax to PHP

I'm trying to post to php using ajax.
I can't seem to figure why the data isn't been posted.
The console.log shows 'success'.
var obj shown is for check only.
the code:
var obj = {'age':'32'};
obj = JSON.stringify(obj);
$.ajax({
type: 'post',
data: {'phpobj': obj},
dataType: 'json',
success: function(data){
//do whatever.
console.log('success');
}
});
and the php (in the same url):
if (isset($_POST['phpobj'])) {
echo 'phpobj is POSTED:</br></br>';
$php_obj = $_POST['phpobj'];
$decoded = json_decode($php_obj, true);
var_dump($decoded);
} else {
echo 'phpobj Wasnt POSTED';
}
Thanks for helpers.
Please try replacing your code like this
JQUERY
$.ajax({
type: 'post',
data: {'age': 32},
dataType: 'json',
success: function(data){
//do whatever.
console.log('success');
}
});
PHP
if ($_SERVER['REQUEST_METHOD'] == "POST") {
echo 'REQUEST is POSTED:</br></br>';
$age= $_POST['age'];
var_dump($_POST);exit;
} else {
echo 'phpobj Wasnt POSTED';
}
Change the rest according to your requirements. Thanks
i encounterd the same problem before, i gave up using ajax to post.
you can use the jquery to create a form then let the user submit it to php.

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

data binding in Kendo mobile

I am doing a kendo mobile application and I'm trying to bind data from database for listing using json call. I tried with the following code,but its not working Pls help me with this...thanks in advance...
my code is here:
$(document).ready(function () {
var dataSource = new kendo.data.DataSource({
transport: {
read: {
type: "POST",
url: "WebService/listing.php",
contentType: 'application/json; charset=utf-8',
datatype: "json"
}
}
});
dataSource.bind("change", function () {
$("#content").html(kendo.render(template, dataSource.view()));
});
dataSource.read();
console.log(dataSource.view());
});
You could try to use the changefunction of the dataSource directly:
var dataSource = new kendo.data.DataSource({
transport: {
read: {
type: "POST",
url: "WebService/listing.php",
contentType: 'application/json; charset=utf-8',
datatype: "json"
}
},
change: function() {
$("#content").html(kendo.render(template, this.view()));
}
});
Just some things to consider:
I'm assuming your JSON is correct
You're sure you use POST to get the data
your template is as well defined correctly
You're calling dataSource.read() AFTER the data is loaded (to make sure you're doing this, call read() within $(document).ready(function(){dataSource.read();}); and define the dataSource itself first
I guess the last point is the most crucial ;)
That's all I am doing, so if it doesn't work that way there might be some mistake in the data format itself or in the template definition. Anything like console errors?Cheers

Cross Domain JSON Request Failing

I am trying to run this code in my browser's console:
$.ajax({
dataType: 'json',
url: 'http://www.web2pdfconvert.com/engine?curl=http://www.nytimes.com&outputmode=json?callback=?',
success: function (data) {
if(data.resultcode == 1) {
console.log(true);
} else {
console.log(false);
}
},
});
However, I am getting a Cross Domain Request Error. when I try to make a simple JSON request then also same error occurs, because JSON request cannot be made on Cross Domains. however, when you go to this URL:
http://www.web2pdfconvert.com/engine?curl=http://www.nytimes.com&outputmode=json
You'll be able to see the JSON data. However, a key point written in the documentation of this websites API says that:
json - all conversion data are returned as JSON object. Also JSONP cross domain communication supported usign jQuery.
Thanks in advance.
Use jsonp instead:
$.ajax({
dataType: 'jsonp',
url: 'http://www.web2pdfconvert.com/engine?curl=http://www.nytimes.com&outputmode=json',
jsonp: "callback",
success: function (data) {
if(data.resultcode == 1) {
console.log(true);
} else {
console.log(false);
}
},
});