简体   繁体   中英

Converting AJAX request to jQuery

Hi I am completely new to jQuery and also not the strongest with Javascript so I would appreciate some input on modifying this AJAX request to jQuery.

var test = new Array();

var bindThis = {
    url: "sampleHandler.data",
    method: "post",
    }
    mimetype: "text/json",
    content:  test
};
var request1 = dojo.io.bind(bindThis);
dojo.event.connect(request1, "load", this, "ResultsFunction");

My guest is this but I am not 100% sure I have the syntax correct.

var test = new Array();

var bindThis = {
    url: "sampleHandler.data",
    type: "post",
    }
    dataType: "text/json",
    data:  test
};

As for the dojo event handler I haven't been able to find a great resource on how to bind the request. My guest is something along these lines?

$(this).load(function(){"ResultsFunction"})

How am I making out? Thanks in advance.

EDIT: I forgot to add that this is an application that uses both Dojo and Prototype. I am trying to migrate the code to jQuery.

From the jQuery AJAX API

    $.ajax({
        type:       'POST',
        url:        'sampleHandler.data',
        data:       data,
        dataType:   'json',
        success:    function (result)
        {

        }
    });

Alternatively

    $.post("sampleHandler.data", data,
        function(result) {
    }, 
        "json"
    );

More info

If you are going to perform a POST operation, most likely you want to send data that needs to be serialized to a JSON format (if your server operation is expecting that type of data), here's an example:

var dataToSend = {'taco':'yum'};
$.ajax({
        url:'/myurl/',
        dataType:'json',
        contentType: 'application/json',
        data: JSON.stringify(dataToSend),                
        type: 'POST',
        success: function(data){
            // perform operation with the data you receive
           alert('success! received: ' + data);
        }
});

You can get more info if you visit: api.jquery.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM