4

As the title says, can I create a SharePoint-Hosted App and make Ajax calls right away? or do I have to import an ajax library or something like that?

8

Ajax has nothing to do with jQuery per se.

https://en.wikipedia.org/wiki/Ajax_(programming)

http://blog.garstasio.com/you-dont-need-jquery/ajax/

http://caniuse.com/#feat=xhr2

jQuery has a function which wraps around JavaScript native 'Ajax' calls

$.ajax({
    type: "POST",
    url: "webservice",
    data: "a=1&b=2&c=3",
    success: function(d) {
        console.log(d);
    }
});

But this exact functionality can be written without jQuery as:

var r = new XMLHttpRequest(); 
r.open("POST", "webservice", true);
r.onreadystatechange = function () {
    if (r.readyState != 4 || r.status != 200) return; 
    console.log(r.responseText);
};
r.send("a=1&b=2&c=3");
  • 1
    I am in school for another 6 months :D, I tried the XMLHttpRequest, and I realized I'm trying to post/put to a (http) from a (https) and that doesn't work.. does SP.Webproxy solve this? because it worked when I was GETting data from the same webservice using the SP.webproxy. – Dr Cox Sep 16 '15 at 7:55
  • GET and POST are protocols, Ajax only means you can execute them async. The POST protocol has (ofcourse) many security restrictions, search for Cross Domain blogs (and quit school now) – Danny '365CSI' Engelman Sep 16 '15 at 8:51
  • 2
    There are some pitfalls of coding directly against XMLHttpRequest though... especially if you need cross-browser compatibility (which everyone does...) check out the jQuery ajax source code (ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.js -- search for ajax: function( url, options ) Although I like thought of just plugging into RequestExecutor – John-M Sep 21 '15 at 1:40
3

No Its not built in, you need to add reference to jQuery - <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> to the head of the script tag.

You can also use SP.RequestExecutor.jsto replace ajax calls.

executor = new SP.RequestExecutor(siteurl);
executor.executeAsync({...})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.