无法通过salesforce混合应用程序(forceios)调用REST API

时间:2015-02-10 13:37:09

标签: javascript rest salesforce-ios-sdk

我通过forceios创建了一个混合型salesforce应用程序,我可以显示所有联系人。

但是现在我想显示报告所以我使用了REST API,但方法

forcetk.Client.prototype.ajax

没有被调用,xcode或浏览器中没有显示错误。

我和你分享了app.js文件,请帮帮我......

app.js

var forceClient;

(function(){

"use strict";

/* Adding platform (ios/android) specific css */
var platformStyle = document.createElement('link');
platformStyle.setAttribute('rel', 'stylesheet');
if (/Android/.test(navigator.userAgent)) {
    platformStyle.setAttribute('href', 'css/ratchet-theme-android.css');
} else if (/iPhone/.test(navigator.userAgent)) {
    platformStyle.setAttribute('href', 'css/ratchet-theme-ios.css');
}
document.querySelector('head').appendChild(platformStyle);


/* Wait until cordova is ready to initiate the use of cordova plugins and app launch */
document.addEventListener("deviceready", function() {
    authenticateUser(showUsersList);
}, false);

/* Method to authenticate user with Salesforce Mobile SDK's OAuth Plugin */
var authenticateUser = function(successHandler, errorHandler) {

    // Get salesforce mobile sdk OAuth plugin
    var oauthPlugin = cordova.require("com.salesforce.plugin.oauth");

    // Call getAuthCredentials to get the initial session credentials
    oauthPlugin.getAuthCredentials(
        // Callback method when authentication succeeds.
        function (creds) {
            // Create forcetk client instance for rest API calls
            forceClient = new forcetk.Client(creds.clientId, creds.loginUrl);
            forceClient.setSessionToken(creds.accessToken, "v31.0", creds.instanceUrl);
            forceClient.setRefreshToken(creds.refreshToken);

            // Call success handler and handover the forcetkClient
            successHandler(forceClient);


                                   var path = '/v29.0/analytics/reports/00OD0000001ZbP7MAK/instances';

                                   var method = 'POST';

                                   var error = null;

                                   var payload = null;

                                   var retry = null;

                                   alert("1");

                                   forcetk.Client.prototype.ajax = function(path, callback, error, method, payload, retry) {

                                   alert("2");

                                   var that = this;
                                   var url = this.instanceUrl + '/services/data' + path;

                                   return $j.ajax({
                                                  type: method || "GET",
                                                  async: this.asyncAjax,
                                                  url: (this.proxyUrl !== null) ? this.proxyUrl: url,
                                                  contentType: method == "DELETE"  ? null : 'application/json',
                                                  cache: false,
                                                  processData: false,
                                                  data: payload,
                                                  success: callback,
                                                  error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) {
                                                  if (jqXHR.status === 401) {
                                                  that.refreshAccessToken(function(oauthResponse) {
                                                                          that.setSessionToken(oauthResponse.access_token, null,
                                                                                               oauthResponse.instance_url);
                                                                          that.ajax(path, callback, error, method, payload, true);
                                                                          },
                                                                          error);
                                                  } else {
                                                  error(jqXHR, textStatus, errorThrown);
                                                  }
                                                  },
                                                  dataType: "json",
                                                  beforeSend: function(xhr) {
                                                  if (that.proxyUrl !== null) {
                                                  xhr.setRequestHeader('SalesforceProxy-Endpoint', url);
                                                  }
                                                  xhr.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId);
                                                  xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);
                                                  }
                                                  });
                                   }



        },
        function (error) {
            alert('Failed to authenticate user: ' + error);
        }
    );
}

/* This method will render a list of users from current salesforce org */
var showUsersList = function(forceClient) {

    fetchRecords(forceClient, function(data) {
        var users = data.records;
        var name;

        var listItemsHtml = '';
        for (var i=0; i < users.length; i++) {

            name = users[i].Name;

            name = name.replace("'", "\\'");

            listItemsHtml += ('<li onClick="getDetails('+"'"+name+"'"+')"><a href="#page2" data-transition="slide" class="ui-btn ui-btn-icon-right ui-icon-carat-r ui-nodisc-icon">' + users[i].Name+ '</a></li>');
        }

        document.querySelector('#users').innerHTML = listItemsHtml;
    })
}

/* This method will fetch a list of user records from salesforce. 
Just change the soql query to fetch another sobject. */
var fetchRecords = function (forceClient, successHandler) {
    var soql = 'SELECT Id, Name FROM Contact';
    forceClient.query(soql, successHandler, function(error) {
        alert('Failed to fetch users: ' + error);
    });
};

})();

函数回调(){

alert("hello");

}

function getDetails(name){

$("#name").html("");
$("#account").html("");
$("#title").html("");
$("#email").html("");
$("#phone").html("");
$("#fax").html("");
$("#mailingstreet").html("");
$("#mailingcity").html("");
$("#mailingcountry").html("");


name = name.replace("'", "\\'");

var query = "select name, account.name, title, email, phone, fax, mailingstreet, mailingcity, mailingcountry from contact where name='"+name+"'";


forceClient.query( query, function(response){

                  alert(JSON.stringify( response.records[0] ));

                  $("#name").html("Name :" + response.records[0].Name);
                  $("#account").html("Account :" +response.records[0].Account.Name);
                  $("#title").html("Title :" + response.records[0].Title);
                  $("#email").html("Email :" +response.records[0].Email);
                  $("#phone").html("Phone :" + response.records[0].Phone);
                  $("#fax").html("Fax :" +response.records[0].Fax);
                  $("#mailingstreet").html("Mailing Street :" +response.records[0].MailingStreet);
                  $("#mailingcity").html("Mailing City :" + response.records[0].MailingCity);
                  $("#mailingcountry").html("Mailing Country :" +response.records[0].MailingCountry);



             });

}

1 个答案:

答案 0 :(得分:0)