0

I am trying to use AJAX in my project to handle images (load more info and delete). Loading is working, but when i try to delete any of them, I recieve error 500 and php script is not used.

var page = 1;
var selected = [];
var mediaAll = [];
var jsont = JSON.stringify({"action": "loadMedia", "page": page});
AJAXcall(jsont, "loadMedia");

function AJAXcall(data, action){
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    console.log(xhttp);
    if (this.readyState == 4 && this.status == 200) {
      if(action=="loadMedia"){
        var media = JSON.parse(this.responseText);
        mediaAll = [];
        mediaAll = mediaAll.concat(media);
        mediaLoaded();
      }else if(action=="loadMoreMedia"){
        var media = JSON.parse(this.responseText);
        mediaAll = mediaAll.concat(media);
        mediaLoaded();
      }else if(action=="deleteMedia"){
        response = JSON.parse(this.responseText);
        console.log(response);
        page = 1;
        if(response == true){
          AJAXcall(jsont, "loadMedia");
        }
      }
    }
  };
  xhttp.open("POST", "./libs/img_handler.php", true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send(data);
}
function deleteSingle(){   
   if(selected.length === 1){
    if (confirm('sure?')) {
      var dataS = {"action": "deleteMedia", "id": selected[0]};
      dataDel = JSON.stringify(dataS);
      AJAXcall(dataDel, "deleteMedia");
    }
   }
}
function loadMore(){
  page++;
  var jsont = JSON.stringify({"action": "loadMoreMedia", "page": page});
  AJAXcall(jsont, "loadMoreMedia");
}

When used function loadMore

XMLHttpRequest { onreadystatechange: onreadystatechange(), readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, responseURL: "http://localhost:8888/project/libs/img_handler.php", status: 200, statusText: "OK", responseType: "", response: "..."}

When used function deleteSingle

XMLHttpRequest { onreadystatechange: onreadystatechange() , readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, responseURL: "http://localhost:8888/project/libs/img_handler.php", status: 500, statusText: "Internal Server Error", responseType: "", response: "" }

3
  • Can you show us the server side code (PHP code) on `./libs/img_handler.php`? Commented Aug 8, 2018 at 12:41
  • Best guess - there's something in the deleteSingle code that's killing the system... we'd need to see what that is. Commented Aug 8, 2018 at 12:43
  • you don't have to write var media = JSON.parse(this.responseText); for 3 times. Commented Aug 8, 2018 at 12:50

1 Answer 1

-1

You have mentioned that you have not used php script however responseURL seems referring to .php file.

Normally, 500 status code refers to Internal Server Error. Please try to check error log OR debug your code.

This will help you to get cause of error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.