jQuery AJAX get fields back from PHP - php

The following code sends some data to a PHP file. That PHP file encodes to a JSON object and echoes three variables error, success, and action.
However, neither the success function here, nor the error function appear to be returning that JSON object.
How would one get the values for these three keys back.
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: ua['url'],
data: { sconnect: 'email', email : $('#semail').val(), hack:86 },
dataType: 'json',
success: function (data) {
alert(data['success']); alert(data['error']); alert(data['action']);
},
error: function(data){
alert(data['success']); alert(data['error']); alert(data['action']);
}
});

Related

CORS Ajax POST variables empty in live server but work in local server

After days (and a bit of spamming here and there), I did get CORS to work for my CodeIgniter applications. However, POST variables are empty in the PHP side in live server. the whole $_POST array is empty for some reason. Here is my ajax code:
var postForm = {'e':'sammy'}; //data to process
$.ajax({
type: "POST",
url: "http://www.abc.ca/types/add",
data: postForm,
dataType : "json",
cache: "false",
contentType: "application/json",
success: function (result) {
//result here is blank
alert(result);
},
fail: function (result){
alert(result);
}
});
and the called php function (without the whole controller class):
function add() {
//Add new a biz type
file_put_contents('trial.txt',implode(" | ",$_POST));
echo $this->input->post('e');
}
Am I missing something? the response from server are ok. but still the file created contains no data and echo prints empty. Any tips please? I have already tried method:'POST' instead of type:'POST'. Get variables pass away successfully.
Is this CI issue or Ajax because it is constant across ff,chrome and Opera
Try This Ajax :
$.ajax({
type: "POST",
url: "http://www.abc.ca/types/add",
data: postForm,
cache: "false",
success: function (result) {
//result here is blank
alert(result);
},
fail: function (result){
alert(result);
}
});

$.ajax and method post not work correctly

I have a simple code-form like this:
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
When on PHP server page, I try to print $_POST["action"] yet this field is blank. Why does it work fine if I use $.post(url,data,function)?
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
method: "login"
},
success: function (data) {
console.log(data);
}
});
login is a php function
You may use method instead of type.
$.ajax({
url: "http://myurl/test.php",
method: "POST", // <-- Use method, not type
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
Problem is solved.....
There is a problem with the callback on success event...and yesterday I focused my attention only on the request php in the developers tool....when I used Curl (with the right request) all work correctly and I forgot to check client code in success callback....
Never use post method with developers console,it's very easy to lose more time :=)

AJAX request returning error

I have an AJAX request that is throwing an error.
It's part of a larger system that I've inherited, so I can't post the files in their entirety, but I've tried to isolate the relevant code:
$.ajax(
{
url: ajax_url, // Valid URL
data: jsonData, // Valid JSON
dataType: 'json',
contentType: 'application/json',
type: POST,
async:true,
success: function(data)
{
console.log(data);
parsedData = JSON.parse(data);
console.log(parsedData);
// Here I am trying to log successful output
},
error: function(jsonData)
{
console.log("error");
console.log(jsonData);
}
});
In the PHP, values are added to an array and returned:
$data['status']=200;
$data['new_id']=$insert_id;
return json_encode($data);
All I am logging at the moment is 'error'. Apologies again for incomplete code, I have tried to supply the code where I think the error is caused.

pass array with ajax to server

I try to pass ids as array via ajax but when I dump out the get variable on server side with PHP I do not get any values, the array is empty. Debugging with firebug I see that the array is getting passed but [] is encoded ids%5B%5D
my javascript
function update_category(selected) {
$.ajax({
url: '/admin/?controller=products&action=update_category',
type: 'GET',
dataType: "application/JSON",
data: {
ids: cat_ids,
s_category: selected
},
success: function(data) {
addAlert('alert-'+data, data);
},
error: function(data) {
addAlert('alert-'+data.responseText, data.responseText);
}
});
}
var_dump $_GET['ids'] null dumping the hole $_GET I get ["ids%5B%5D"]
I do not understand why the array is getting encoded on submit in linux environment
Under ubuntu 12.10 PHP 5.3.10 Firebug XHR->PARAMS
action update_category
controller products
ids%5B%5D 1403172219
ids%5B%5D 1530542001
s_category 1
how to pass properly the array?
Since you are already passing GET values int he URL here:
/admin/?controller=products&action=update_category
Then the information in data probably never passes.
Try this instead:
function update_category(selected) {
$.ajax({
url: '/admin/',
type: 'GET',
dataType: "application/JSON",
data: {
ids: cat_ids,
s_category: selected,
controller: 'products',
action: 'update_category'
},
success: function(data) {
addAlert('alert-'+data, data);
},
error: function(data) {
addAlert('alert-'+data.responseText, data.responseText);
}
});
}

Jquery ajax response error

Here is a partial example of what I am trying to achieve.
I am trying to retrieve a value from ajax but the javascript result.success variable is undefined. I have the following:
PHP:
$result = array ("success" => null, "html" => "");
$result['success'] = true;
$result['html'] = "test";
echo json_encode($result);
Javascript/jQuery:
var ID = 1
$.ajax({
type: 'POST',
url: '/ajax/load.php',
contentType: "application/json; charset=utf-8",
datatype: 'json',
data: {ID: ID},
beforeSend: function(xhrObj) {
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Accept","application/json");
},
success: function(result) {
if (result.success) {
// do something
}
});
The response I am getting from ajax (retrieved from chrome dev tools) is {"success":true,"html":"Test"}
This looks fine to me however in the JavaScript result.success is undefined. I believe this will be simple I just can't see where the issue lies..
Your error is here:
datatype: 'json',
Javascript is case sensitive and the property is dataType:
dataType: 'json',
Because of that, jQuery is not being told to automatically parse the JSON, so the result is just treated as html or plain text.
You also need to remove the content-type header because that specifies the content type for the request not response. You are sending form/url encoded in the request, not JSON.
$.ajax({
type: 'POST',
url: '/ajax/load.php',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: {ID: ID},
beforeSend: function(xhrObj) {
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Accept","application/json");
},
success: function(result) {
if (result.success) {
// do something
}
} // Maybe your forgot this
});
in other words - Basic Debugging
in your PHP page put this on top of the page (you are accepting only json response but probably your response headers content type is text/html
header('Content-type: application/json');

Categories