0

I search for a solution, and there are some answers but none solve my problem. I want to add variable to the end of url which is user text input value (this is number of ID to delete in database).

var idRem = parseInt($('#removeId').val(),10);

            $removeBtn.on('click',function(){

                $.ajax({
                      type: 'DELETE',
                      url: "http://rest.learncode.academy/api/kuba/friends/"+idRem,
                      success: function() {
                        //no data...just a success (200) status code
                        console.log('Friend Deleted Successfully!');
                      },
                      error: function() {
                    alert('Deleting error');
                    }
                    });
                });

I see only error message, but when I put manually the value to the url or assign to variable then it works.

Is there anyone knows what is wrong?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kuba0506
  • 455
  • 2
  • 8
  • 20
  • What error message you are getting, you probably need to get the removeId value in click handler. – Adil Aug 22 '14 at 08:58
  • DELETE http://rest.learncode.academy/api/kuba/friends/NaN 400 (Bad Request), there is Nan instead of number even though I used parseInt – kuba0506 Aug 22 '14 at 09:00
  • Have you checked value of `idRem` variable? – Prashant16 Aug 22 '14 at 09:01
  • it seems that the problem is in this line: `var idRem = parseInt($('#removeId').val(),10);` idRem is not getting a numerical value - hence - Not a Number (NaN) – Reins Aug 22 '14 at 09:02
  • When I type this in console var temp = parseInt($('#removeId').val(),10); I get number not string.typeof temp = "number" – kuba0506 Aug 22 '14 at 09:03

3 Answers3

0

That's how I do it, and that's working..

$(document).ready(function(){

    $('#btnAddToWatchlist').click(function(){

        location.href="<?php print Yii::app()->createUrl('Products/AddToWatchList'); ?>?id="+<?php print $productid ?>;

    });

});
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Lucian
  • 23
  • 4
0

Considering that you are getting a NaN in your idRem variable then i think the problem is WHEN you are calling the initialization. I would try moving the var idRem = parseInt($('#removeId').val(),10); into the function call. Problem might be that the parseInt is called on pageload when there is no value in the #removeId element.

Reins
  • 1,109
  • 1
  • 17
  • 35
  • When I declare 'idRem' with other variables and then idRem = parseInt($('#removeId').val(),10) in a function, there is ReferenceError: idRem is not defined – kuba0506 Aug 22 '14 at 09:27
  • try declaring + initializing it in the function. – Reins Aug 22 '14 at 16:58
0

The DELETE method requests that the origin server delete the resource identified by the Request-URI.

$.ajax({
url: "http://rest.learncode.academy/api/kuba?" + $.param({"friends": idRem}),
type: 'DELETE',
success: function() {
//no data...just a success (200) status code
console.log('Friend Deleted Successfully!');
},
error: function() {
alert('Deleting error');
}
});