Zend Framework: Ajax post data - ajax

That is my function:
function deleteAutoAd(id, title) {
alert(title);
$.ajax({
dataType: 'json',
url: '/ajax/deleteautoad',
type: 'POST',
data: {
id : id,
title : title
},
success: function(data) {
alert(data);
}
});
}
In this function when I try alert(title) and popup 'This is title'(because title = 'This is title'). But in ajax/deleteautoad I try this:
public function deleteautoadAction() {
if ($this->getRequest()->isPost()) {
echo $param1 = $this->_request->getParam('id');
echo $param2 = $this->_request->getParam('title');
}
}
Echo param1 show 5, but echo param2 don't show nothing.

i think you should try :
function deleteAutoAd(id, title) {
alert(title);
$.ajax({
dataType: 'json',
url: '/ajax/deleteautoad',
type: 'POST',
data: {
'id' : id, // Note the quotes
'title' : title // Note the quotes
},
success: function(data) {
alert(data);
}
});
}
regards
mimiz

I agree with the above answer
Also, For my application, I also have to post the full url. So you might want to try
var root = location.protocol + '//' + location.host;
and then on your url
url: root+'/ajax/deleteautoad',

You should return from controller a valid JSON with Success status.
public function deleteautoadAction() {
if ($this->getRequest()->isPost()) {
$x->Status = 'Success';
$x->Message = $this->_getParam('id') . $this->_getParam('title');
$this->_helper->json($x);
}
}
And also you should change your alert statement
function deleteAutoAd(id, title) {
$.ajax({
dataType: 'json',
url: '/ajax/deleteautoad',
type: 'POST',
data: {
id : id,
title : title
},
success: function(data) {
alert(data.Message);
}
});
}

Moved from the OP question to a CW answer:
I fixed the problem by removing: dataType: 'json'.

Related

ajax is not sending data to controller

ajax is not sending data to controller
var url = "<?php echo base_url(); ?>";
function allowip() {
var url = "<?php echo base_url(); ?>"
var id = $('#path').attr('value');
$.ajax({
type: "POST",
url: url+"client/home/get_ip",
//dataType: 'text',
data: {'id': id},
//cache: false,
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
alert is showing with data but not sending data to controller
controller is empty
public function get_ip($id)
{
$day = $this->input->post('id');
echo $day;
}
As your get_ip function have argument $id so it must need to pass on it, url keyword might be conflicting so use different variable name and get the value from #path with val() function.
It might help:
function allowip() {
var id = $('#path').val();
var request_url = "<?php echo base_url(); ?>"+"client/home/get_ip/"+id;
$.ajax({
type: "POST",
url: request_url,
data: {'id': id},
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
public function get_ip($id)
{
echo $id;
}
or you can do is to post $id in data as you're already doing it but only have to change some lines:
function allowip() {
var id = $('#path').val();
var request_url = "<?php echo base_url(); ?>"+"client/home/get_ip/";
$.ajax({
type: "POST",
url: request_url,
data: {'id': id},
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
public function get_ip()
{
$day = $this->input->post('id');
echo $day;
}
You are sending a parameter to a function so you can change your code like this
function allowip() {
var id = $('#path').attr('value');
var url = "<?php echo base_url(); ?>"+"client/home/get_ip/"+id
$.ajax({
type: "GET",
url: url,
dataType: "html",
success: function(abc) {
alert(abc);
},
error:function(data) {
alert('no working');
}
});
}
and your action will be
public function get_ip($id)
{
echo $id;
}
I think it is probably the way you are getting the value. It's hard to tell without seeing the view code, but I will assume that $('#path') is an <input...> element of some sort. If that is correct then try getting the value like this
var id = $('#path').val();
Forget about using an argument with the get_ip() function. You are posting to the controller not creating a URL with argument values. Try this.
public function get_ip()
{
$day = $this->input->post('id');
echo !empty($day) ? $day : "No Input"; //or, an empty string instead?
}

Sending url using ajax on codeigniter

I have this Ajax script where I pass a link to the data variable link, but I
get a 412 error.
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {
link: $(this).data('link')
},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
I have tried
link: encodeURI($(this).data('link'))
And
link: encodeURIComponent($(this).data('link'))
as is suggested on other threads but I still get the 412 error message.
Hope this will help you :
you have added newline character to json data that is why you got error
Do like this :
var link = $(this).data('link');
data: {"link" : link},
/*----OR do this -----*/
data: {"link" : $(this).data('link')},
instead of this :
data: {
link: $(this).data('link')
},
Whole code should be like this :
var link = $(this).data('link');
/*console.log(link)*/
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"link" : link },
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
For More :https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412
Please change your code in data options. Use this way
data: {"id": ID},
i.e. store the value in a variable and send that variable in data option. If we assume
ID=$(this).data('link');`
Then the code will be as follows:
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"id":ID},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
Please check it.

Pass array to ajax request in $.ajax()

I have tried like this but could not get array list. It returns 'null'
var data=[];
data[0] = '1';
data[1] = '2';
$.ajax({
url: '#Url.Action("AddFrequencyWeekly", "Plannar")',
type: "POST",
data: { data: data },
dataType: 'json',
success: function (data) {
alert("Record Updated Successfully");
}
});
my model class code following
public JsonResult AddFrequencyWeekly(string[] data)
{
}
data value says 'null'
please help me?
var datas = { data0: '1',
data1: '2' };
$.ajax({
url: '#Url.Action("AddFrequencyWeekly", "Plannar")',
type: "POST",
data: datas,
dataType: 'json',
success: function (data) {
alert("Record Updated Successfully");
}
});
Its the way you are expecting to recive the data in the action method,
try
public JsonResult AddFrequencyWeekly(IEnumeable<string> data)
{
}

display values returned by json

I have simple question to display data on html page. Following code displays array of json data on screen. but, I want to display it by each element such as "url", "img_url" and so on.
could you please let me know who to do it ?
ajax code
var dataString = 'url=' + pathname + '&img_name=' + img_name + "&tag=" + tag;
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
complete: function (xhr, status) {
if (status === 'error' || !xhr.responseText) {
//handleError();
alert("error");
} else {
var data = xhr.responseText;
$('#tt').html("<div id='message'></div>");
$('#message').html(data);
}
}
});
json return
{"cid":"14","url":"http:\/\/localhost\/","img_url":"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg","img_name":"mat_173317134c.jpg","html":"<div id=\"hotspot-19\" class=\"hs-wrap hs-loading\">\r\n<img src=\"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg\">\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"95\" data-y=\"64\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nasdf\r\n<\/div>\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"168\" data-y=\"53\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nrere\r\n<\/div>\r\n<\/div>\r\n","jscript":""}
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
success: function (data) {
for(var item in data){
console.info(item);//print key
console.info(data[item]);//print value
}
}
});
I hope this is what you need.

asp.net mvc 3 json does not work

This is my jquery with json
$('#btnVerificationOk').click(function () {
var verId = $('#trans_verification_id').val();
var verCode = $('#trans_verification_code').val();
$.ajax({
url: '/Profile/CompleteTransactions',
type: 'POST',
data: { },
dataType: 'json',
success: function (result) {
alert(result);
},
error: function () {
alert('ERROR ERROR !!!!!!!!!!!!');
}
});
});
And My C# method:
[Authorize]
[HttpPost]
private JsonResult CompleteTransactions()
{
return Json("Done");
}
Its always alerts 'ERROR ERROR !!!!!!!!!!!!' i tried debugging but CompleteTransactions method is not firing
And this is my second json which is bellow and works good
$('#btnTransfareOk').click(function () {
var userName = $('#transfare_username').val();
var amount = $('#transfare_amount').val();
if (userName == '' || amount == '') {
$('#transfare_error_list').html('Please fill boxes.');
} else {
$.ajax({
url: '/Profile/TranfareMoney',
type: 'POST',
data: { ToUsername: userName, Amount: amount },
dataType: 'json',
success: function (result) {
$('#transfare_error_list').html(result.text);
$('#trans_verification_id').val(result.id);
$('#transfare_username').val("");
$('#transfare_amount').val("");
},
error: function () {
$('#transfare_error_list').html('Oops... Error.');
}
});
}
});
I'm not 100% sure, but shouldn't you controller action handler be public ?

Resources