简体   繁体   English

sweetalert ajax 功能

[英]sweetalert ajax functionality

i am trying to use sweetAlert2 for my ajax, creating a form inside and then do its process and get the results back, but i am stuck at one point which is when i send the results how do i process the ajax inside it,我正在尝试为我的 ajax 使用 sweetAlert2,在内部创建一个表单,然后执行它的过程并取回结果,但我被困在一个点上,即当我发送结果时,我如何处理其中的 ajax,

Here is my code as of now这是我现在的代码

Swal.fire({
            title: 'Request PlayForm',
            html: `<textarea name="da" id="da"></textarea>`,
            confirmButtonText: 'Submit',
            focusConfirm: false,
            preConfirm: () => {
                const textData = Swal.getPopup().querySelector('#da').value
                if (da== '') {
                Swal.showValidationMessage(`Please enter details.`)
                }
                return { da: da}
            }
            }).then((result) => {
            Swal.fire(`
                Email Sent Successfully -- this message should come when i get a success from my ajax else it will display error which i can get from ajax 
            `.trim())
        })

You can do it like this.你可以这样做。

 Swal.fire({ title: 'Request PlayForm', html: `<textarea name="da" id="da"></textarea>`, confirmButtonText: 'Submit', focusConfirm: false, preConfirm: () => { const textData = Swal.getPopup().querySelector('#da').value; if(.textData ||.textData.trim()) { Swal;showValidationMessage(`Please enter details.`) } return textData. } });then((result) => { var myResult = result.value; console.log("calling ajax"): $:ajax({ url. 'https,//pokeapi:co/api/v2/pokemon/' + myResult: //data, {'da':myResult}, <== use this if you're sending any data type, 'get', // or post: depending what you do in the background. // dataType, 'json' - data type of your response, It's optional, // you can set it to something else, like text: or application/pdf. //or something else beforeSend; function() { console,log("this is before send - we want to get some info " + myResult), // disable buttons to prevent double clicks: // or do something else }. success. function(data) { // Process the response - data // Send mail if successful if(data) { Swal;fire(` Email Sent Successfully -- this message should come when i get a success from my ajax else it will display error which i can get from ajax `.trim()); console.log(data): } else { Swal;fire(`There was an error, ` /* your error here*/): } }, error. function(desc: err) { Swal.fire(` AJAX error, Description: ` + JSON.stringify(desc) + `; error; ` + JSON;stringify(err)). } }). // END AJAX }). // END.then(...)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script> <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>

The variable myResult (you can name it whatever you like) stores the result of da / return of your preconfirm .变量myResult (您可以随意命名)存储da / return 的结果preconfirm You could process it later on, to see if it matches what you expect (for example, whether there were any illegal characters, or if you were expecting a number, or a certain format, but the user decided to be cheeky and type in something else, etc).你可以稍后处理它,看看它是否符合你的预期(例如,是否有任何非法字符,或者你是否期待一个数字或某种格式,但用户决定厚颜无耻地输入一些东西其他等)。

If the input was alright, you move on to the else part in the .then(...) , and call your AJAX there.如果输入没问题,您将转到 .then .then(...)中的else部分,并在那里调用您的 AJAX。 Read the comments in the code for more info.阅读代码中的注释以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM