This is the angular code i used to post data
const postData = {
"username": "Customer004",
"email": "customer004#email.com",
"password": "0000252525",
"gender": "male",
}
this.http.post("http://localhost/android/Api.php?apicall=signup", postData, options)
.subscribe(data => {
console.log(data);
});
But when i tried to print the response i am getting null in the backend using php code
response['post']=$_POST['username'];
it is giving me null value
this is my php code..
I have hardcoded the data.But trying to print post request.It is giving me null
This is my php code
https://pastebin.com/raw/VishzAvG
My angular code is here
https://pastebin.com/raw/LNk3KvYW
At the backend in php use
$post=json_decode(file_get_contents('php://input'));
echo $post->username;
It'll work
Related
I'm having a trouble, why I'm getting this error in my app flutter main.dart
I just follow this reference link. It would be great if anybody could figure out, thank you so much in advance!.
Here's the error part
Future<List> _login() async {
final response = await http.post("http://10.0.2.2/my_store/login.php", body: {
"username": user.text,
"password": pass.text,
});
You need to parse your URLs now, use it like this:
Future<List> _login() async {
final response = await http.post(Uri.parse("http://10.0.2.2/my_store/login.php"), body: {
"username": user.text,
"password": pass.text,
});
I'm writing a server side application for the jquery plugin DataTables, since it's not working I simplified my implementation.
This is the js part:
$('#foo').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": uri_api,
"method": "POST",
"data": function(d) {
d.arguments="get_foo"
}
},
"columns": [
{ "data": "ONE" },
{ "data": "TWO" },
{ "data": "THREE" }
]
});
And this is my api.php:
<?php
header("Access-Control-Allow-Origin: *");
$data = json_decode(file_get_contents('php://input'), TRUE);
set_error_handler("error_handler", E_ALL);
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {http_response_code(200);exit();}
var_dump($data);
var_dump(file_get_contents('php://input'));
var_dump(json_last_error_msg());
die();
As you can see it's a very basic try, this is my real anser of api.php:
NULL
string(12259) "draw=1&columns%5B0%5D%5Bdata%5D=ID&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=false&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=Hostname&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=IP&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=IP_List&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=Service&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=Management_Platform&columns%5B5%5D%5Bname%5D=&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=true&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B6%5D%5Bdata%5D=Service_Owner&columns%5B6%5D%5Bname%5D=&columns%5B6%5D%5Bsearchable%5D=true&columns%5B6%5D%5Borderable%5D=true&columns%5B6%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B6%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B7%5D%5Bdata%5D=Service_Area&columns%5B7%5D%5Bname%5D=&columns%5B7%5D%5Bsearchable%5D=true&columns%5B7%5D%5Borderable%5D=true&columns%5B7%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B7%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B8%5D%5Bdata%5D=Service_SOM&columns%5B8%5D%5Bname%5D=&columns%5B8%5D%5Bsearchable%5D=true&columns%5B8%5D%5Borderable%5D=true&columns%5B8%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B8%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B9%5D%5Bdata%5D=Service_Reference&columns%5B9%5D%5Bname%5D=&columns%5B9%5D%5Bsearchable%5D=true&columns%5B9%5D%5Borderable%5D=true&columns%5B9%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B9%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B10%5D%5Bdata%5D=Classes&columns%5B10%5D%5Bname%5D=&columns%5B10%5D%5Bsearchable%5D=true&columns%5B10%5D%5Borderable%5D=true&columns%5B10%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B10%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B11%5D%5Bdata%5D=Instance&columns%5B11%5D%5Bname%5D=&columns%5B11%5D%5Bsearchable%5D=true&columns%5B11%5D%5Borderable%5D=true&columns%5B11%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B11%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B12%5D%5Bdata%5D=Instance_Address&columns%5B12%5D%5Bname%5D=&columns%5B12%5D%5Bsearchable%5D=true&columns%5B12%5D%5Borderable%5D=true&columns%5B12%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B12%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B13%5D%5Bdata%5D=TAG&columns%5B13%5D%5Bname%5D=&columns%5B13%5D%5Bsearchable%5D=true&columns%5B13%5D%5Borderable%5D=true&columns%5B13%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B13%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B14%5D%5Bdata%5D=Env&columns%5B14%5D%5Bname%5D=&columns%5B14%5D%5Bsearchable%5D=true&columns%5B14%5D%5Borderable%5D=true&columns%5B14%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B14%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B15%5D%5Bdata%5D=Status&columns%5B15%5D%5Bname%5D=&columns%5B15%5D%5Bsearchable%5D=true&columns%5B15%5D%5Borderable%5D=true&columns%5B15%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B15%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B16%5D%5Bdata%5D=Domain&columns%5B16%5D%5Bname%5D=&columns%5B16%5D%5Bsearchable%5D=true&columns%5B16%5D%5Borderable%5D=true&columns%5B16%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B16%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B17%5D%5Bdata%5D=DC&columns%5B17%5D%5Bname%5D=&columns%5B17%5D%5Bsearchable%5D=true&columns%5B17%5D%5Borderable%5D=true&columns%5B17%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B17%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B18%5D%5Bdata%5D=Role&columns%5B18%5D%5Bname%5D=&columns%5B18%5D%5Bsearchable%5D=true&columns%5B18%5D%5Borderable%5D=true&columns%5B18%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B18%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B19%5D%5Bdata%5D=Service_Platform&columns%5B19%5D%5Bname%5D=&columns%5B19%5D%5Bsearchable%5D=true&columns%5B19%5D%5Borderable%5D=true&columns%5B19%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B19%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B20%5D%5Bdata%5D=OS&columns%5B20%5D%5Bname%5D=&columns%5B20%5D%5Bsearchable%5D=true&columns%5B20%5D%5Borderable%5D=true&columns%5B20%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B20%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B21%5D%5Bdata%5D=SW&columns%5B21%5D%5Bname%5D=&columns%5B21%5D%5Bsearchable%5D=true&columns%5B21%5D%5Borderable%5D=true&columns%5B21%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B21%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B22%5D%5Bdata%5D=Baseline&columns%5B22%5D%5Bname%5D=&columns%5B22%5D%5Bsearchable%5D=true&columns%5B22%5D%5Borderable%5D=true&columns%5B22%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B22%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B23%5D%5Bdata%5D=Cluster&columns%5B23%5D%5Bname%5D=&columns%5B23%5D%5Bsearchable%5D=true&columns%5B23%5D%5Borderable%5D=true&columns%5B23%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B23%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B24%5D%5Bdata%5D=Last_Communication&columns%5B24%5D%5Bname%5D=&columns%5B24%5D%5Bsearchable%5D=true&columns%5B24%5D%5Borderable%5D=true&columns%5B24%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B24%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B25%5D%5Bdata%5D=Compliance&columns%5B25%5D%5Bname%5D=&columns%5B25%5D%5Bsearchable%5D=true&columns%5B25%5D%5Borderable%5D=true&columns%5B25%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B25%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B26%5D%5Bdata%5D=ACS&columns%5B26%5D%5Bname%5D=&columns%5B26%5D%5Bsearchable%5D=true&columns%5B26%5D%5Borderable%5D=true&columns%5B26%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B26%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B27%5D%5Bdata%5D=LMS&columns%5B27%5D%5Bname%5D=&columns%5B27%5D%5Bsearchable%5D=true&columns%5B27%5D%5Borderable%5D=true&columns%5B27%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B27%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B28%5D%5Bdata%5D=EPO&columns%5B28%5D%5Bname%5D=&columns%5B28%5D%5Bsearchable%5D=true&columns%5B28%5D%5Borderable%5D=true&columns%5B28%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B28%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B29%5D%5Bdata%5D=ACS_DB&columns%5B29%5D%5Bname%5D=&columns%5B29%5D%5Bsearchable%5D=true&columns%5B29%5D%5Borderable%5D=true&columns%5B29%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B29%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B30%5D%5Bdata%5D=ACS_MW&columns%5B30%5D%5Bname%5D=&columns%5B30%5D%5Bsearchable%5D=true&columns%5B30%5D%5Borderable%5D=true&columns%5B30%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B30%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B31%5D%5Bdata%5D=LMS_DB&columns%5B31%5D%5Bname%5D=&columns%5B31%5D%5Bsearchable%5D=true&columns%5B31%5D%5Borderable%5D=true&columns%5B31%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B31%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B32%5D%5Bdata%5D=LMS_MW&columns%5B32%5D%5Bname%5D=&columns%5B32%5D%5Bsearchable%5D=true&columns%5B32%5D%5Borderable%5D=true&columns%5B32%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B32%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B33%5D%5Bdata%5D=LMS_L285&columns%5B33%5D%5Bname%5D=&columns%5B33%5D%5Bsearchable%5D=true&columns%5B33%5D%5Borderable%5D=true&columns%5B33%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B33%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B34%5D%5Bdata%5D=RMT_Update&columns%5B34%5D%5Bname%5D=&columns%5B34%5D%5Bsearchable%5D=true&columns%5B34%5D%5Borderable%5D=true&columns%5B34%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B34%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B35%5D%5Bdata%5D=ACS_OS_Update&columns%5B35%5D%5Bname%5D=&columns%5B35%5D%5Bsearchable%5D=true&columns%5B35%5D%5Borderable%5D=true&columns%5B35%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B35%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B36%5D%5Bdata%5D=LMS_OS_Update&columns%5B36%5D%5Bname%5D=&columns%5B36%5D%5Bsearchable%5D=true&columns%5B36%5D%5Borderable%5D=true&columns%5B36%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B36%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B37%5D%5Bdata%5D=EPO_Update&columns%5B37%5D%5Bname%5D=&columns%5B37%5D%5Bsearchable%5D=true&columns%5B37%5D%5Borderable%5D=true&columns%5B37%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B37%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B38%5D%5Bdata%5D=ACS_DB_Update&columns%5B38%5D%5Bname%5D=&columns%5B38%5D%5Bsearchable%5D=true&columns%5B38%5D%5Borderable%5D=true&columns%5B38%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B38%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B39%5D%5Bdata%5D=ACS_MW_Update&columns%5B39%5D%5Bname%5D=&columns%5B39%5D%5Bsearchable%5D=true&columns%5B39%5D%5Borderable%5D=true&columns%5B39%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B39%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B40%5D%5Bdata%5D=LMS_DB_Update&columns%5B40%5D%5Bname%5D=&columns%5B40%5D%5Bsearchable%5D=true&columns%5B40%5D%5Borderable%5D=true&columns%5B40%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B40%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B41%5D%5Bdata%5D=LMS_MW_Update&columns%5B41%5D%5Bname%5D=&columns%5B41%5D%5Bsearchable%5D=true&columns%5B41%5D%5Borderable%5D=true&columns%5B41%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B41%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B42%5D%5Bdata%5D=LMS_L285_Update&columns%5B42%5D%5Bname%5D=&columns%5B42%5D%5Bsearchable%5D=true&columns%5B42%5D%5Borderable%5D=true&columns%5B42%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B42%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B43%5D%5Bdata%5D=Host_Source&columns%5B43%5D%5Bname%5D=&columns%5B43%5D%5Bsearchable%5D=true&columns%5B43%5D%5Borderable%5D=true&columns%5B43%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B43%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B44%5D%5Bdata%5D=Instance_Source&columns%5B44%5D%5Bname%5D=&columns%5B44%5D%5Bsearchable%5D=true&columns%5B44%5D%5Borderable%5D=true&columns%5B44%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B44%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B45%5D%5Bdata%5D=IP_Source&columns%5B45%5D%5Bname%5D=&columns%5B45%5D%5Bsearchable%5D=true&columns%5B45%5D%5Borderable%5D=true&columns%5B45%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B45%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B46%5D%5Bdata%5D=Service_Source&columns%5B46%5D%5Bname%5D=&columns%5B46%5D%5Bsearchable%5D=true&columns%5B46%5D%5Borderable%5D=true&columns%5B46%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B46%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B47%5D%5Bdata%5D=Host_ID&columns%5B47%5D%5Bname%5D=&columns%5B47%5D%5Bsearchable%5D=true&columns%5B47%5D%5Borderable%5D=false&columns%5B47%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B47%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B48%5D%5Bdata%5D=Instance_Host_ID&columns%5B48%5D%5Bname%5D=&columns%5B48%5D%5Bsearchable%5D=true&columns%5B48%5D%5Borderable%5D=false&columns%5B48%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B48%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B49%5D%5Bdata%5D=Asset_ID&columns%5B49%5D%5Bname%5D=&columns%5B49%5D%5Bsearchable%5D=true&columns%5B49%5D%5Borderable%5D=false&columns%5B49%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B49%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B50%5D%5Bdata%5D=Service_ID&columns%5B50%5D%5Bname%5D=&columns%5B50%5D%5Bsearchable%5D=true&columns%5B50%5D%5Borderable%5D=false&columns%5B50%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B50%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B51%5D%5Bdata%5D=Link_Service_ID&columns%5B51%5D%5Bname%5D=&columns%5B51%5D%5Bsearchable%5D=true&columns%5B51%5D%5Borderable%5D=false&columns%5B51%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B51%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B52%5D%5Bdata%5D=Record_Date&columns%5B52%5D%5Bname%5D=&columns%5B52%5D%5Bsearchable%5D=true&columns%5B52%5D%5Borderable%5D=true&columns%5B52%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B52%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B53%5D%5Bdata%5D=Record_Status&columns%5B53%5D%5Bname%5D=&columns%5B53%5D%5Bsearchable%5D=true&columns%5B53%5D%5Borderable%5D=true&columns%5B53%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B53%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B54%5D%5Bdata%5D=RowNum&columns%5B54%5D%5Bname%5D=&columns%5B54%5D%5Bsearchable%5D=true&columns%5B54%5D%5Borderable%5D=false&columns%5B54%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B54%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&arguments=get_presentation"
string(12) "Syntax error"
I don't know why is formatted like this, it looks having html entities, "%5B" should be "[". I don't know to convert and the php function are not working. I have used yet datatables before, and never noticed this. If I just do var_dump($_POST) I see the right structure, but why php://input doesn't work?
EDIT: just to develop i'm using windows 10, xampp php 8.0.2 and webpack without a main web server. I'm just browsing the page result of npm run build with Chrome.
EDIT2: I solved with parse_str in this way
if (JSON_ERROR_NONE !== json_last_error()) {
parse_str($datajson,$data);
}
My question is kept, I would like solve without parse_str.
Ok I finally solved with json data.
https://datatables.net/forums/discussion/26282/posting-json-with-built-in-ajax-functionality
Based on the link above I modified my js in this way:
$('#foo').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": uri_api,
"method": "POST",
"data": function(d) {
return JSON.stringify($.extend(d, { "arguments" : "get_foo"}));
}
},
"columns": [
{ "data": "ONE" },
{ "data": "TWO" },
{ "data": "THREE" }
]
});
I didn't figure out that default data type were not json, in my previous implementation I used another js library instead of datatables, I used tabulator.
Since I got experience with datatables I choose this library for this new project which will be develop with webpack, and in my previous datatables implementation I really was not using a backend like now with api and a proper instance but just standalone ad hoc scripts and the script I was using for the serverside pagination of datatables was written with $_POST input, not json as I thought. I forgot this detail. Whereas tabulator was using natively the json data. Well, thank you very much.
Regards.
I want to Post a Json string or PHP array to a Restful API and get the response without reloading the page. So, I want to use Ajax and/or jquery. So far, I'm failing miserably. my code is below.
I've tried several jquery methods - some using stringify and others just sending raw json. I'm not getting enough response or error of any kind to know where I need to look to solve the problem.
$(".avm_zip").on("click", function() {
$.ajax({
url: "REMOVED",
type: "POST",
data: {
"Header": {
"UserId": "REMOVED",
"Password": "REMOVED",
"SourceApplication": "Dev Service Tester"
},
"Property": {
"StreetAddress": "10345 W Briar Oaks Dr # E",
"City": "Stanton",
"State": "CA",
"Zip": "90680",
"APN": null,
"PropertyType": null
},
"Order": {
"CustomerOrderReference": "Test LITE",
"PreferenceTableName": "VeroLITE TEST",
"Price": null,
"Credit": null,
"AdditionalFields": null
}
},
dataType:'json',
success: function (response) {
alert("Success");
},
error: function(error){
alert("Something went wrong");
},
});
The API is more robust and centered on XML. Getting the basic call & retrieval set up using Json had challenges because of this. So, it is possible that is bleeding over into this effort.
I'm trying to use Data-tables but I need to pass a value from Ajax to PHP file.
the Ajax part is like this:
<script>
$(document).ready(function() {
var oTable =
$('#user-list').DataTable({
"serverSide": true,
"ajax": {
"url": "assets/server_processing_reminders.php",
"data": {
"CurrentFlag": 1
}
},
"columnDefs": [{
"width": "6%",
"targets": 0
}],
"order": [
[1, "asc"]
]
});
});
</script>
on the server side Im trying to get the variable "CurrentFlag" using:
<?php
if (isset($_GET["CurrentFlag"])){
$cf = $_GET["CurrentFlag"];
}
echo $cf;
but the php file is not printing out the value send.
Thanks for any help
Please use $_REQUEST instead of $_GET like this :
if(isset($_REQUEST["CurrentFlag"]))
{
$cf = $_REQUEST["CurrentFlag"];
}
echo $cf;
OR
If you want print data using $_GET method please add type:GET under ajax call
You need to provide request type as GET as shown here.
"ajax" : {
"url": "assets/server_processing_reminders.php",
type: "GET",
"data": {
"CurrentFlag": 1
}
}
Use Jquery for better code and results.
Learn Ajax using this link :
https://www.w3schools.com/js/js_ajax_intro.asp
I've a php script that has the following line:
echo json_encode(array('success'=>'true','userid'=>$userid, 'data' => $array));
It returns the following:
{
"success": "true",
"userid": "1",
"data": [
{
"id": "1",
"name": "Trigger",
"image": "",
"subtitle": "",
"description": "",
"range1": null,
"range2": null,
"range3": null
},
{
"id": "2",
"name": "DWS",
"image": "",
"subtitle": "",
"description": "",
"range1": null,
"range2": null,
"range3": null
}
]
}
But when I call a jQuery ajax as below:
$.ajax({
type: 'POST',
url: 'url',
crossDomain: true,
data: {name: name},
success: function(success, userid, data) {
if (success = true) {
document.write(userid);
document.write(success);
}
}
});
The userid is 'success'. The actual success one works, its true.
Is this malformed data being returned? Or is it simply my code?
Thanks in advance,
Niall
The arguments the success callback takes are defined in the documentation:
success(data, textStatus, jqXHR)
The response is not split up before being passed as arguments to it. You have to extract the values from the first argument.
You also need to add header('Content-Type: application/json') to your PHP so that jQuery will parse the response as JSON instead of as HTML when populating the first argument.
You can then test data.success and access data.userid (as well as data.data which will be the array assigned to the data property in the returned data… you might want to rename it to avoid confusion).
Note that when you test it you need to use === (or ==), not the * assignment* operator you are using at present. Also note that your JSON response is returning the string "true" and not the boolean true.
You can't add your own arguments in Success. Change your code like this,
success: function(response) {
if (response.success == true) {
document.write(response.userid);
document.write(response.success);
}
}
According to jQuery Docs for success(),
success(data, textStatus, jqXHR)
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn.
http://api.jquery.com/jQuery.ajax/