Azure mobile service invokeApi not working from cordova project

独自空忆成欢 提交于 2019-12-12 03:23:40

问题


I'm using Azure Mobile Services(AzMS) in VisualStudio JS-Apache Cordova project where I call a AzMS's custom API to insert user data in storage. This very same code I have previously used in a VS web app javascript and there it is working fine. However here in the cordova project, I get an "unexpected connection failure" error when calling invokeApi. When I try from VS web app, it works fine, which means the custom API service code is good.

Here is my js client code:

  azmsClient.login(oAuthProvider).done(function (results) {
       console.log("You are now logged in as: " + results.userId);
       var theUserAuthId = results.userId;

        azmsClient.invokeApi('Users/insert', {
               method: 'POST',
                body: { userAuthId: theUserAuthId }
         }).done(function (response) {
                //.... success code
            }, 
            function (error) {
                console.log("Error: " + err.request.responseText);
                //.... error handling
            }); 

  }, 
  function (err) {
    console.log("Error: " + err.request.responseText);
    //.... error handling
  });

In the console log, the first log ("You are now logged in as: "..) gets logged, after that the error - unexpected connection failure.
And my azure custom Api code -

var logger = require('../api/logUtils.js').logger;

exports.register = function(api){
  api.post('insert', insertUser);
};

/******************************************************************
* @param request
* @param response
*******************************************************************/
function insertUser(request, response){
  var user = request.user;
  var iM = "api.User.insertUser-";
  logger.info( iM + ' called: - ' , request, response, user);
  // Data validation
  if ( user.level === 'anonymous' ) {
    logger.error( iM + 'Anonymous User' );
    response.send(500, { error: "Anonymous user." });
  }

  user.getIdentities({
    success: function (identities) {
        var req = require('request');

        var userId = user.userId.split(':')[1];
        var theProvdr = user.userId.split(':')[0];
        var reqParams;

        logger.info(iM + ': calling getOAuthUserDetails for Identities: - ' , identities);
        try {
            reqParams = getOAuthUserDetails(userId, identities);
        }
        catch(err){
            logger.error(iM + ': getOAuthUserDetails  - ' , err);
            response.send(500, { error: err.message });
            return;
        }

        req.get(reqParams, function (err, resp, body) {
                if (err) {
                    logger.error(iM + ': Error calling provider: ', err);
                    response.send(500, { error: 'Error calling Authentication provider' });
                    return;
                }

                if (resp.statusCode !== 200) {
                    logger.error(iM + ': Provider call did not return success: ', resp.statusCode);
                    response.send(500, { error: 'Provider call did not return success: ' + resp.statusCode });
                    return;
                }

                try {
                    logger.info(iM + ': success: got User Details body ', body);
                    var theAppUser = oAuthUser_To_appUser(theProvdr, JSON.parse(body));

                    addUser(theAppUser, user, {
                        success: function(userAlreadyExist, userEnt){
                            logger.info( iM + ': addUser: success', userEnt);
                            response.send(200, getAppUserEnt(userEnt));
                        },
                        error: function(err){
                            logger.error( iM + ': Error in addUser: ', err);
                            response.send(500, { error: err.message });
                        }
                    });

                } catch (err) {
                    logger.info(iM + ': Error parsing response: ', err);
                    response.send(500, { error: err.message });
                }
            });
    },
    error: function(err){
        logger.info(iM + ': error on calling getIdentities: - ' , err);
        response.send(500, { error: err.message });
    }
});



In the azure service logs, I see no entry logged from the custom api's user.insert function when running from the cordova project, which means the api is got getting called. Like said before, when calling from VS web project, the log records look all good.
(this is somewhat similar to the issue asked here, but not exactly the same.)
I am unable to figure out why its happening so; any idea?


回答1:


Are you running on a device, emulator, or Ripple. If its Ripple, you have to change the Cross Domain Proxy to 'disabled'. I had similar issues and this seemed to help.



来源:https://stackoverflow.com/questions/27698480/azure-mobile-service-invokeapi-not-working-from-cordova-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!