JavaScript How to change url of ajax function depending the return value of beforesend() function? ajax change function,ajax change function jquery

I want to run a function inside beforesend() in jquery ajax. Depending on the return value of that function I want to change the URL of ajax request. For an example refer below.

If myFunction() returns a value grater than 1, I want to run url1 otherwise I want to run url2. How to achieve that?

myFunction() gives a value grater than 1. But always ajax runs the url2.

$.ajax({
    type: "POST",
    data: {
        fname: $('#com').val()
    },
    dataType: "json",
    beforeSend: function () {
        myFunction($('#com').val())
    },
    url: (myFunction($('#com').val()) > 1) ? url1 : url2,
    success: function (data) {
        if (data == 'success') {
            window.location.href = 'index.php?r=site/index';
        } else {
            alert("Already registered email");
        }
    },
    failure: function (errMsg) {
        alert(errMsg);
    }
});
Answer:1

try this

$.ajaxSetup({
    beforeSend: function(jqXHR, settings) {
       settings.url ="new Url";
    }
});
Answer:2

Create a global variable something like:

var urlToSend;

and then assign it in beforeSend

$.ajax({
    type: "POST",
    data: {
        fname: $('#com').val()
    },
    dataType: "json",
    beforeSend: function () {
        urlToSend=myFunction($('#com').val()) > 1 ? url1 : url2;
    },
    url: urlToSend,
    success: function (data) {
        if (data == 'success') {
            window.location.href = 'index.php?r=site/index';
        } else {
            alert("Already registered email");
        }
    },
    failure: function (errMsg) {
        alert(errMsg);
    }
});
Answer:3

Here's what I've done so far. Setup S3 with a free account, hardcoded my access keys with AWS.config.update({ accessKeyId: 'hidden', secretAccessKey: 'hidden' }); AWS.config.region = 'us-west-...

Here's what I've done so far. Setup S3 with a free account, hardcoded my access keys with AWS.config.update({ accessKeyId: 'hidden', secretAccessKey: 'hidden' }); AWS.config.region = 'us-west-...

I need to change the background color of particular parent element in an each. html: <template name="List"> <div class="listclass"> {{#each namelist}} <div class="panel"> ...

I need to change the background color of particular parent element in an each. html: <template name="List"> <div class="listclass"> {{#each namelist}} <div class="panel"> ...

  1. parent element from iframe
  2. parent element from child
  3. parent element from dom
  4. parent from element
  5. parent element in javascript
  6. parent element in jquery
  7. parent element in css
  8. parent element in xpath
  9. parent element in html
  10. parent element in js
  11. parent element with class javascript
  12. parent element in selenium
  13. parent element in xml
  14. parent element in pom.xml
  15. parent element with class jquery
  16. parent element in protractor
  17. parent element in maven
  18. parent element in typescript
  19. parent element in react
  20. parent element in angularjs

I'm trying to learn some of the <canvas> API right now. I've tasked myself with creating a simple analog style clock with working clock hands (second, minute and hour). The clock frame, face ...

I'm trying to learn some of the <canvas> API right now. I've tasked myself with creating a simple analog style clock with working clock hands (second, minute and hour). The clock frame, face ...

For complex functions declared within a loop, I can see why I wouldn't want to do this, but why would it be be considered bad javascript? We can name the function and place it outside the loop of ...

For complex functions declared within a loop, I can see why I wouldn't want to do this, but why would it be be considered bad javascript? We can name the function and place it outside the loop of ...

  1. functions declared within loops referencing an outer
  2. functions declared within loops referencing
  3. functions declared within loops referencing an outer scoped
  4. functions declared within loops
  5. creating functions within loops
  6. functions within a loop
  7. jshint functions declared within loops
  8. jshint functions declared within loops referencing
  9. javascript functions declared within loops