score:0

here is an approach to handle api request using redux-saga:

first create a request helper

import 'whatwg-fetch';

function parsejson(response) {
 return response.json ? response.json() : response;
}

/**
 * checks if a network request came back fine, and throws an error if 
   not
 *
 * @param  {object} response   a response from a network request
 *
 * @return {object|undefined} returns either the response, or throws an 
 * error
*/
 function checkstatus(response, checktoken = true) {
   if (response.status >= 200 && response.status < 300) {
     return response;
  }

 return parsejson(response).then(responseformatted => {
   const error = new error(response.statustext);
   error.response = response;
   error.response.payload = responseformatted;
   throw error;
 });
}

/**
 * requests a url, returning a promise
 *
 * @param  {string} url       the url we want to request
 * @param  {object} [options] the options we want to pass to "fetch"
 *
 * @return {object}           the response data
*/
export default function request(url, options = {}) {
// set headers
  if (!options.headers) {
    options.headers = object.assign({
     'content-type': 'application/json',
   }, options.headers, {});
  }

 // stringify body object
 if (options && options.body) {
   options.body = json.stringify(options.body);
 }

 return fetch(url, options)
  .then(checkstatus)
  .then(parsejson)
}

in your saga

import { call, fork, put, takelatest } from 'redux-saga/effects';
import request from 'utils/request';

import { submitsuccess, submiterror } from './actions'; // path 
to your actions.

import { submit } from './constants'; // the event you're listening

export function* submitdata(action) {
  try {
    const response = yield call(request, 'your_url', { method: 'post', body: action.body });
    yield put(submitsuccess(response));
  } catch(err) {
     yield put(submiterror(response.payload.message);
  }
}

export function* defaultsaga() {
  yield fork(takelatest, submit, submitdata);
}

export default defaultsaga;

reducer

const initialstate = fromjs({
  submitsuccess: false,
  submitreponse: '',
  errormessage: '',
});

 function fooreducer(state = initialstate, action) {
   switch (action.type) {
     case submit_success:
       return state
         .update('submitsuccess', () => true)
         .update('submitresponse', () => action.response);
     case submit_error:
       return state.update('errormessage', () => action.errormessage);
     //...
   }
  }

with this structure you should be able to catch your success and you error when you're making your request.

score:1

you shouldn't be defining the success in your api request. $.ajax will return a promise on its own:

const firstapirequest = () => (
  $.ajax({
    url: myurl,// ,
    type:'post',
    headers:{
        "accept":"application/json",
        "content-type":"application/json",
    },
    data:json.stringify(bodydata),
}));

also, why are you using jquery for making the api requests? i'd suggest using axios or fetch


Related Query

More Query from same tag