Home > other >  Sending a static text in ajax
Sending a static text in ajax

Time:07-09

I'm making an image reload page

<button id="ref_qo" ><a href="#" style="text-decoration: none !important;">Reload question</a></button>

But this is not done correctly

<script type="text/javascript">
$(document).ready(function() {
$('#ref_qo').click(function(e) {

e.preventDefault();
$.ajax({

type: "GET",
url: 'image_lod.php',
data: { id: 'image_url', IdCompanyAnbar: '12'},

success: function(response){
var resp = JSON.parse(response);

if (resp.success == "0"){
alert(resp.message)
} else {
alert(resp.message);
}
}
});
});
});
</script>

and gives the following error

Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at Object.success ((index):334:24) at i (jquery.min.js:2:28017) at Object.fireWith [as resolveWith] (jquery.min.js:2:28783) at A (jquery.min.js:4:14035) at XMLHttpRequest. (jquery.min.js:4:16323)

I don't think there is a problem with the server

<?php
if (isset($_GET['image_url']) {

$url = $_GET['image_url'];
    // Save all user data
if($url == 12){
echo json_encode(array('success' => 0, 'message' => 'Please complete both fields'));
} else {
echo json_encode(array('success' => 1));
}
}
?>

Thank you in advance for your help!

CodePudding user response:

the proble: in this condition if (isset($_GET['image_url']) {....

1: you didn't send the image_url to the server.

2: and missing the ) in the if condition.

You can modify the code on the server as follows

<?php
if (isset($_GET['id']) && $_GET['id']=='image_url') {
     $url = $_GET['IdCompanyAnbar']??0;
     // Save all user data
     if($url == 12){
         echo json_encode(array('success' => 0, 'message' => 'Please complete both fields'));
     } else {
         echo json_encode(array('success' => 1));
     }
}else{
         echo json_encode(array('success' => 1));
}
?>
  • Related