none
Callback function and jsonp RRS feed

  • Question

  • Hello,

    I am developing an App for Office for Project Pro. I am trying to do cross domain on the Project Data API using JQuery Ajax function with the jsonp parameter. The Ajax call raises an error that the callback function wasn't fired even if I pass as parameter the name of a jsonpCallback function.

    Does anybody know if ProjectData handles jsonp and the name of the callback function ?

    Here is my code :

    var accept = "application/json; odata=verbose";
        accept.toLocaleLowerCase();
        // Enable cross-origin scripting (required by jQuery 1.5 and later).
        // This does not work with Project Online.
        $.support.cors = true;
        // this part is pretty straightforward, we set the mandatory data we need and the handle the success and error cases.
        $.ajax({
            url: restUrl,
            type: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: 'jsonp',
            crossDomain: true,
            data: "",      // Empty string for the optional data.
            beforeSend: function (xhr) {
                xhr.setRequestHeader("ACCEPT", accept);
            },
            success: function (data, textStatus, jqXhr) {
                //...
            },
            error: function (data, textStatus, errorThrown) {
                throwError("An error occured", errorThrown);
            }
        });

    Wednesday, April 24, 2013 4:37 PM

All replies

  • Ajax jsonp call also needs to be supported on server side. Can you show the code of how the web service returns the response?

    Basically the Ajax jsonp call adds an additional parameter callback to web service call, which you need to use to wrap up the response.

    private string AppendJsonpCallback(string json, HttpRequest request)
    {
        return request.Params["callback"] +"(" + json + ");";
    }
    

    More details and an example is here .


    Anze Javornik

    Thursday, April 25, 2013 3:54 AM
  • It is an App for Office so there is no server side. It is full Javascript and HTML. Moreover, the web service is provided by Microsoft so I can't touch it. The ProjectData API is provided my Microsoft to access the reporting database of a given project server instance.

    That is why I was asking if anybody knew if Microsoft handled callbacks for jsonp on ProjectData and if they wrapped the response in a callback.

    Thank you.

    Thursday, April 25, 2013 8:33 AM
  • Ofcourse there is no server side code in Apps for Office....but you can add it to wrap the Microsoft Web Service if it is not jsonp supported.

    Create a web service that calls the web service in code behind and then handle the response to jsonp format.


    Anze Javornik

    Thursday, April 25, 2013 8:38 AM
  • Reading your other thread in this forum i noticed you have a problem, since ProjectData services are on intranet and then the proxy described above, would also not work, since app hosting server would not be able to reach the intranet.

    Reading on ajax documentation there might be a way to call it like this: (jQuery 1.5 or above)..i dont think it will work, but if you cannot make a server side proxy and if the web service does not support jsonp, then this might be the only way. Or if the web service supports CORS and you limit your app to CORS supported browsers, then you can use dataType: 'json' with crossDomain: true. (IE10, Firefox, Chrome, ... / IE older then 10 do not support CORS).

    $.ajax({ url: 'http://cross-domain.com/the_jsonp_file', jsonp : false, jsonpCallback: 'jsonCallback', cache: 'false', dataType : 'jsonp',

    success: function(json) {
    // Process
         console.log(json.test);
        },
    }); function jsonCallback(data) { // handle response }

    Relevant documentation:

    For jsonp parameter:

    As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

    For jsonCallback parameter:

    As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.

    (This might just call the callback function wihtout the actual response).


    Anze Javornik

    Thursday, April 25, 2013 10:07 AM
  • Hi Anze,
    thanks for answering my question and even checking my other threads !

    I have tried your solution but unfortunately it doesn't work. I've tried to change some parameters and tweak the ajax request without any luck.
    I tried making XMLHttpRequest instead of jquery, it works locally but not in real conditions with an online server and a local instance.

    I am really baffled because I started my project from the Project 2013 SDK for Office Apps and most of the changes I made are about treating the data not the request.
    This is supposed to be the basics of a Project Pro Application. You develop an app for Project Pro so you access the given project server data (if you have the good rights) and treat it in a interesting way for users.

    I don't see the point of doing an app for Project pro that call Youtube webservices (MS example), Bing Map (MS example) or converts currencies (it is one of the 5 apps for project Pro on the store).

    Anyway, this is my point of view. I really appreciate that you took the time to read and answer my questions. If you have more ideas don't hesitate
    Thursday, April 25, 2013 4:50 PM
  • If you try these http://terrasus.com/detail.jsp?articleID=396 step by step it will work fine. if you produce jsonp response you should get the callback value and set it to your response dynamically. This article has a detail explanation
    Thursday, June 12, 2014 10:35 AM