sarpay
10/28/2019 - 5:56 PM

Ajax Operations

Custom AJAX Call

Sample 1

import { XmlHttpRequestHelper } from '@shared/helpers/XmlHttpRequestHelper';

private static linkedAccountAuthenticate(
  switchAccountToken: string,
  tenantId: number,
  callback: () => void): void {

    abp.multiTenancy.setTenantIdCookie(tenantId);
    const cookieLangValue = abp.utils.getCookieValue('Abp.Localization.CultureName');
  
    let requestHeaders = {
      '.AspNetCore.Culture': ('c=' + cookieLangValue + '|uic=' + cookieLangValue),
      'Abp.TenantId': abp.multiTenancy.getTenantIdCookie()
    };
  
    XmlHttpRequestHelper.ajax(
      'POST',
      AppConsts.remoteServiceBaseUrl + '/api/TokenAuth/LinkedAccountAuthenticate?switchAccountToken=' + switchAccountToken,
      requestHeaders,
      null,
      (response) => {
          let result = response.result;
          abp.auth.setToken(result.accessToken);
          AppPreBootstrap.setEncryptedTokenCookie(result.encryptedAccessToken);
          location.search = '';
          callback();
      }
    );
}

Sample 2

private static getUserConfiguration(callback: () => void): any {

  const cookieLangValue = abp.utils.getCookieValue('Abp.Localization.CultureName');
  const token = abp.auth.getToken();

  let requestHeaders = {
    '.AspNetCore.Culture': ('c=' + cookieLangValue + '|uic=' + cookieLangValue),
    'Abp.TenantId': abp.multiTenancy.getTenantIdCookie()
  };

  if (token) {
    requestHeaders['Authorization'] = 'Bearer ' + token;
  }

  return XmlHttpRequestHelper.ajax(
    'GET', 
    AppConsts.remoteServiceBaseUrl + '/AbpUserConfiguration/GetAll', 
    requestHeaders, 
    null, 
    (response) => {
      let result = response.result;
      _.merge(abp, result);
      abp.clock.provider = this.getCurrentClockProvider(result.clock.provider);
      abp.event.trigger('abp.dynamicScriptsInitialized');
      DynamicResourcesHelper.loadResources(callback);
    }
  );
}

Server Response Object

using Abp.Web.Models;

try
{
  if (file == null)
  {
    throw new UserFriendlyException(L("File_Empty_Error"));
  }
  // ...
  return Json(new AjaxResponse(new
  {
    id = fileObject.Id,
    name = file.FileName,
    contentType = file.ContentType
  }));
}
catch(UserFriendlyException ex) {
  return Json(new AjaxResponse(new ErrorInfo(ex.Message)));
}

public AjaxResponse();
public AjaxResponse(bool success);
public AjaxResponse(object result);
public AjaxResponse(ErrorInfo error, bool unAuthorizedRequest = false);

public ErrorInfo();
public ErrorInfo(string message);
public ErrorInfo(int code);
public ErrorInfo(int code, string message);
public ErrorInfo(string message, string details);
public ErrorInfo(int code, string message, string details);