JavaScript调用Ajax请求之前

huangapple go评论70阅读模式
英文:

javascript calling before ajax call request

问题

I have a problem in my coding, I made it very very short so that I could get a fast answer, and not to waste your time, I have an input text, an input number and a button, when I click a button, a function would call named (getvalue), but the problem is the ajax call run after the javascript code, the big project that am working on, one of the ajax call, is that if the other php file returns invalid, then I don't want to apply .val() in the input text with id (num), cause if it's invalid, and the calculation is wrong, why should the javascript code work?!, I don't want to be run if the ajax returns invalid, that's all I have some ajax call requests inside this function in my project and need to run and call before any other javascript codes, how could I do that?!

here is the index file php code:







Document

<input type="text" name="" id="inp">
<button onclick="getValue()">Click Me</button>

<br>
<input type="text" name="" id="num">

<script src="jquery-3.6.1.min.js"></script>
<script>
    function getValue() {

        var inpText = $("#inp").val();
        var inpNum = $("#num");
        alert("hey ")
        alert("hello world ")
        //   javascript coding..... aproximately 100 lines

        inpNum.val("random num...")

        $.ajax({
            type: 'POST',
            url: 'validate.php',
            data: {
                myInput: inpText,
            },
            complete: function(r) {
                if (r.responseText == "validNum") {
                    alert("valid")
                } else {
                    alert("invalid number, please enter another number  ")
                }

            }
        });       
    }
</script>



again, I made it very simple, just so you understand, I don't want to delete validate.php file and put the whole code inside index.php, with all other ajax call requests.

英文:

I have a problem in my coding, I made it very very short so that I could get a fast answer, and not to waste your time, I have an input text, an input number and a button, when I click a button, a function would call named (getvalue), but the problem is the ajax call run after the javascript code, the big project that am working on, one of the ajax call, is that if the other php file returns invalid, then I don't want to apply .val() in the input text with id (num), cause if it's invalid, and the calculation is wrong, why should the javascript code work?!, I don't want to be run if the ajax returns invalid, that's all I have some ajax call requests inside this function in my project and need to run and call before any other javascript codes, how could I do that?!

here is the index file php code:

    &lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

    &lt;input type=&quot;text&quot; name=&quot;&quot; id=&quot;inp&quot;&gt;
    &lt;button onclick=&quot;getValue()&quot;&gt;Click Me&lt;/button&gt;

    &lt;br&gt;
    &lt;input type=&quot;text&quot; name=&quot;&quot; id=&quot;num&quot;&gt;

    &lt;script src=&quot;jquery-3.6.1.min.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
        function getValue() {

            var inpText = $(&quot;#inp&quot;).val();
            var inpNum = $(&quot;#num&quot;);
            alert(&quot;hey &quot;)
            alert(&quot;hello world &quot;)
            //   javascript coding..... aproximately 100 lines

            inpNum.val(&quot;random num...&quot;)


            $.ajax({
                type: &#39;POST&#39;,
                url: &#39;validate.php&#39;,
                data: {
                    myInput: inpText,
                },
                complete: function(r) {
                    if (r.responseText == &quot;validNum&quot;) {
                        alert(&quot;valid&quot;)
                    } else {
                        alert(&quot;invalid number, please enter another number  &quot;)
                    }

                }
            });       
        }
    &lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;
&lt;?php

again, I made it very simple, just so you understand, I don't want to delete validate.php file and put the whole code inside index.php, with all other ajax call requests.

答案1

得分: 1

以下是翻译好的内容:

$.ajax({
  type: 'POST',
  url: 'validate.php',
  data: {
    myInput: inpText,
  },
  complete: function(r) {
    if (r.responseText == "validNum") {
      alert("有效")
    } else {
      alert("无效的数字,请输入另一个数字")
    }
  }
}).then(function getValue() {
  var inpText = $("#inp").val();
  var inpNum = $("#num");
  alert("嘿")
  alert("你好,世界")
  // JavaScript 代码... 大约100行
  inpNum.val("随机数字...")
});
英文:

You can execute js code after ajax with then()

$.ajax({
  type: &#39;POST&#39;,
  url: &#39;validate.php&#39;,
  data: {
  myInput: inpText,
  },
  complete: function(r) {
    if (r.responseText == &quot;validNum&quot;) {
    alert(&quot;valid&quot;)
  } else {
    alert(&quot;invalid number, please enter another number  &quot;)
  }
 }
}).then(function getValue() {
  var inpText = $(&quot;#inp&quot;).val();
  var inpNum = $(&quot;#num&quot;);
  alert(&quot;hey &quot;)
  alert(&quot;hello world &quot;)
  //   javascript coding..... aproximately 100 lines
  inpNum.val(&quot;random num...&quot;)
});

答案2

得分: 1

async function myFunction() {
// This function will be called before the AJAX request is sent.
}

async function fetchData() {
const response = await fetch('/api/endpoint');
const data = await response.json();
return data;
}

const data = await fetchData();

英文:
async function myFunction() {
  // This function will be called before the AJAX request is sent.
}

async function fetchData() {
  const response = await fetch(&#39;/api/endpoint&#39;);
  const data = await response.json();
  return data;
}

const data = await fetchData();

huangapple
  • 本文由 发表于 2023年4月6日 22:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定