Friend
Friend

Reputation: 1346

Jquery Ajax issue,not taking options value

Hello People here is my code below...

$.fn.myajax = function (options) {

    var defaults = {
        my_event: "",
        my_url: "",
        my_data: "",
    }
    var o = {};
    var mydata = options.my_data;
    $.extend(o, defaults, options);

    return this.each(function () {
        $(this).bind(options.my_event, function (event) {

            $.ajax({
                url: options.my_url,
                global: false,
                type: "GET",
                data: ({
                    mydata: $('#' + this.id).val()
                }),
                cache: false,
                success: function (html) {
                    console.log('Done Baby :)');
                }
            });
        });
    });
};

In Ajax Data Field data: ({mydata:$('#'+this.id).val()}), it is passing 'mydata' as get variable not options.my_data I also tried , data: ({options.my_data:$('#'+this.id).val()}),but got some error so had to stored in variable but not working...it is passing an url something like thishttp://my_domain.com/serverpage?mydata=friendBut iam expectinghttp://my_domain.com/serverpage?name=friendwherenameis inoptions.my_data; I have already tried data: {'mydata':$('#' + this.id).val()} and also checked this question enter link description here

But did not solve my problem...

Upvotes: 1

Views: 60

Answers (2)

Buzinas
Buzinas

Reputation: 11725

I don't know if I got your question, but it seems you'd like to get the string inside your 'mydata' variable, is that right?

For example, if mydata is a string "smallball", you'd like to have:

 data: ({
      smallball: $('#' + this.id).val()
 }),

EDIT, correct answer:

 var dataAux = {};
 dataAux[mydata] = $('#' + this.id).val();

Inside your jQuery:

 data: dataAux;

JSFiddle: http://jsfiddle.net/KusmV/

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173542

You have to build the data array separately, because it's not something you can do with array literals:

var to_send = {};
to_send[options.my_data] = $(this).val();

...
$.ajax({
    ...
    data: to_send
    ...
});

Upvotes: 1

Related Questions