8 Replies - 3150 Views - Last Post: 16 February 2018 - 09:33 AM

#1 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Query works, but logs an error!

Posted 15 February 2018 - 10:16 AM

$(document).ready(function(){
	$('.deleteUser').on('click', deleteUser);
});

function deleteUser(){
	var confirmation = confirm('Are you sure?');
	if(confirmation) {
		console.log('deleting... /users/delete/'+$(this).data('id'));
		$.ajax({
			type: 'delete',
			'url': '/users/delete/'+$(this).data('id')
		});
		window.location.replace('/');
		console.log('deleted.');
	} else {
		return false;
	}
}


Error in the console:
VM1200 jquery-3.3.1.min.js:2 XHR failed loading: DELETE "http://localhost:3000/users/delete/5a85a2f8e6b34fd39c387c42".
send @ VM1200 jquery-3.3.1.min.js:2
ajax @ VM1200 jquery-3.3.1.min.js:2
deleteUser @ VM1201 main.js:9
dispatch @ VM1200 jquery-3.3.1.min.js:2
y.handle @ VM1200 jquery-3.3.1.min.js:2
10:16:10.559 


I've also tried:
		var del = '/users/delete/'+$(this).data('id');
		$.ajax({
			type: 'delete',
			'url': del
		}).done(function() {
			console.log('done');
			window.location.replace('/');
		});


but while that reports no error, it never refreshes the page or logs 'done'.

Edit: Note that in both cases the user is deleted from the db, so the call is being made and succeeding.

This post has been edited by CTphpnwb: 15 February 2018 - 10:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Query works, but logs an error!

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Query works, but logs an error!

Posted 15 February 2018 - 11:46 AM

This appears to be an access control issue.
If I delete two different users using just the jQuery and then reload, I get this in the console:
13:39:54.102 XHR failed loading: DELETE "http://localhost:3000/users/delete/5a85cb65dabc46dbf3643883".
13:39:54.102 XHR failed loading: DELETE "http://localhost:3000/users/delete/5a85cb65dabc46dbf3643882".
13:39:54.111 Navigated to http://localhost:3000/

Now to find out how to set the appropriate header in Nodejs.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Query works, but logs an error!

Posted 15 February 2018 - 12:05 PM

Then again, maybe it's a browser issue. I'm only seeing it in Chrome. Safari and Firefox Developer Edition don't show it.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Query works, but logs an error!

Posted 15 February 2018 - 12:17 PM

Then again, maybe it's a browser issue. I'm only seeing it in Chrome. Safari and Firefox Developer Edition don't show it.

Edit: Apparently Chrome does have strict security settings by default.

This post has been edited by CTphpnwb: 15 February 2018 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2997
  • View blog
  • Posts: 11,542
  • Joined: 03-December 12

Re: Query works, but logs an error!

Posted 16 February 2018 - 08:52 AM

What is the API returning? It looks like it is expecting something back, that it never gets
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Query works, but logs an error!

Posted 16 February 2018 - 09:10 AM

I've tried it returning nothing, true, and the object:
app.delete('/users/delete/:id', function(req, res){
	db.users.remove({ _id: ObjectId(req.params.id) }, function(err, result){
				if(err) {
					console.log('err',err);
				} else {
					console.log('result',result);
				}
	});
});


This is Nodejs with Mongodb.

I think the problem is solved here, but I don't see how to apply it to my code yet.
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2997
  • View blog
  • Posts: 11,542
  • Joined: 03-December 12

Re: Query works, but logs an error!

Posted 16 February 2018 - 09:14 AM

BUT, you get the 'done' log in other browsers?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Query works, but logs an error!

Posted 16 February 2018 - 09:25 AM

No. If I did I'd be more confident that it's just a Chrome issue. It seems I need to set the correct response headers, but I don't know what they are or where to set them in Nodejs.
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2997
  • View blog
  • Posts: 11,542
  • Joined: 03-December 12

Re: Query works, but logs an error!

Posted 16 February 2018 - 09:33 AM

Set CORs Node?

Actually, this is probably what you want
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1