I am trying to create a simple API for my project. I am sending a DELETE request using jquery Ajax. The Delete request gets sent, does what it is supposed to do(deletes an entry from the database), return a status 200, but fires an error event.
I have already looked for solutions on these posts but they haven't been able to help me:
Ajax request returns 200 but error event is fired Ajax request returns 200 OK but error event is fired Ajax delete returns 200 but firing off error event
This is the AJAX code:
function ajaxCall(method,hmm){ // function that send an ajax request
$.ajax({
dataType: 'JSON',
url: '/APIHandler.php?' + $.param({values:hmm}),
type: method,
success: function(response) { // when the request is done delete the previously placed products for new ones
console.log(response);
const parentDiv = document.querySelector('#basic-grid');
removeAllChildNodes(parentDiv);
turnToObjects(response);
},
error: function(xhr){
alert(xhr);
console.log(xhr);
}
});
}
...
ajaxCall('DELETE',checked);
The APIHandler code:
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
$vals = $_GET['values'];
$API->delete($vals);
}
The request response:
abort: ƒ (e)
arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.r (<anonymous>:1:83)]
caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.r (<anonymous>:1:83)]
length: 1
name: "abort"
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: jquery.min.js:2
[[Scopes]]: Scopes[3]
always: ƒ ()
catch: ƒ (e)
done: ƒ ()
fail: ƒ ()
getAllResponseHeaders: ƒ ()
getResponseHeader: ƒ (e)
overrideMimeType: ƒ (e)
pipe: ƒ ()
progress: ƒ ()
promise: ƒ (e)
readyState: 4
responseText: ""
setRequestHeader: ƒ (e,t)
state: ƒ ()
status: 200
statusCode: ƒ (e)
statusText: "OK"
then: ƒ (t,n,r)
I also can't find any solutions for the error
[Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.r (<anonymous>:1:83)]
Add contentType:'application/json' to your code
And change dataType to text
...
dataType: 'text',
contentType:'application/json'
...
You set your dataType as JSON so $.ajax is attempting to parse your ajax response as JSON but your response is empty so an error is thrown.
Either send a JSON response from your request or remove the dataType parameter.