Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Jquery ajax json response?

Tags:

json

jquery

ajax

:have an ajax request looking like this :

$.ajax({
          url: "/users/action/",
          type: "POST",
          data: myData,
          context: this,
          error: function () {},
          success : function () {
        $(this).removeClass('disabled');
          }
        });

So if the function is successfull, the class "disabled" is removed. However, my function returns the following json :

{"row":"fze684fz6f4ez68f4ze"}

I want to get this value so I can use it later "add it to a data element, i.e I want to add to the clicked element data-row="fze684fz6f4ez68f4ze"

How can I manage this ? I can't figure out by myself, I'm discovering AJAX.

Thanks a lot for your help !

like image 867
Ziplo Avatar asked Feb 03 '26 17:02

Ziplo


1 Answers

It's recommend to set dataType if you excpect to get json . Any way pay attention to the context. It might be a problem with this.

$.ajax({
    url: "/users/action/",
    type: "POST",
    data: myData,
    context: this,
    error: function () {},
    dataType: 'json',
    success : function (response) {
        $(this).removeClass('disabled');
        $(this).data("row",response.row);
    }
});
like image 173
Dvir Avatar answered Feb 06 '26 06:02

Dvir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!