I am trying to send data to server and get back some data in json format using ajax, what i've tried
if (!empty($_REQUEST['data1'])) {
$dt = $_REQUEST['data1'];
$arr = [1,2,3];
header("Content-Type: application/json", true);
echo json_encode($arr);
}
<script>
jQuery(document).ready(function(){
jQuery('.selectpicker').selectpicker();
jQuery('#datepick').datepicker({
format: "dd/mm/yyyy"
});
jQuery('.client_list a').click(function() {
alert(jQuery(this).text());
var data={};
data['client_name']=jQuery(this).text();
load_data(data,window.location.pathname);
});
});
var load_data=function(data,url,selector){
jQuery.post(url,{'data1':data})
.done(function(result){
console.log(result+"Hello");
})
.fail(function( jqXHR, textStatus) {
console.log( "failed due:"+ textStatus);
});
};
</script>
I'm getting parseerror why is is this
UPDATE
my php script and jquery are in one file and my url is window.location.pathname inside getJSON
When jQuery receives a JSON response from an AJAX request it will automatically deserialise it for you. The error you're seeing is because you're calling parseJSON
on an object, you just need to remove that call:
jQuery.getJSON(url,{'data1':data})
.done(function(result){
console.log(result + "Hello");
})
.fail(function( jqXHR, textStatus) {
console.log( "failed due:"+ textStatus);
});
if (!empty($_REQUEST['data1'])) {
$dt = $_REQUEST['data1'];
$arr = [1,2,3];
header("Content-Type: application/json", true);
echo json_encode($arr);
}
change your code because you are sending data by using get method and receiving through post.
Javascript
console.log(data);
jQuery.getJSON(url,{'data1':data})
.done(function(result){
console.log(result + "Hello");
})
.fail(function( jqXHR, textStatus) {
console.log( "failed due:"+ textStatus);
});
IN your console check data is not null by updating this code.