简体   繁体   English

jQuery AJAX与对话框调用

[英]JQuery AJAX with dialog call

There are a couple of points I was hoping for assistance on. 我希望提供几点帮助。 I have the following code, by which I make a ajax call on page load, and either retrieve data successfully or handle any resulting error. 我有以下代码,通过它们我可以在页面加载时进行ajax调用,并且可以成功检索数据或处理任何导致的错误。

I am running JQuery 2.0.0, with JQuery UI 1.10.2 (PHPStorm seems to have trouble recognising later versions). 我正在使用JQuery UI 1.10.2运行JQuery 2.0.0(PHPStorm似乎无法识别更高版本)。

Relevant HTML: 相关HTML:

<script type="text/javascript" src="inc/JQuery-2.0.0.js"></script>
<script type="text/javascript" src="inc/JQuery-UI-1.10.2.js"></script>
<script type="text/javascript" src="inc/core.js"></script>

... ...

    <div id="feedback-dialog">
        <div id="dialog-inner"><!-- dynamic content --></div>
    </div>
</body>

Javascript: Javascript:

var engine = 'engine.php',
    feedbackWrapper = $('#feedback-dialog'),
    feedbackInner = $('#dialog-inner');

function ftShowErrorMessage(e)
{
    var error = e.toString(),
        errorWrapper = $('<p id="error-message"/>');

    $(errorWrapper).text(error);

    $(feedbackInner).empty();
    $(feedbackInner).append(errorWrapper);

    $(feedbackWrapper).dialog({
        title: 'An Error Occurred',
        dialogClass: 'feedback-error',
        buttons: [
            {
                'OK' : function() {
                    //do stuff
                    $(this).dialog('close');
                }
            }
        ]
    });
}

$(document).ready(function()
{
    (function(){

        var args = {};
        args.Request = 'get country ids with county options';

        $.ajax({
            type:   'POST',
            url:    engine,
            data:   args
        })
            .done(function(data)
            {
                try
                {
                    var obj = JSON.parse(data);
                    if(!obj.hasOwnProperty('error'))
                    {
                        //continue
                    }
                    else
                    {
                        ftShowErrorMessage(obj.error);
                    }
                }
                catch(e)
                {
                    ftShowErrorMessage(e)
                }
            })
            .error(function( xhr, ajaxOptions, thrownError )
            {
                ftShowErrorMessage(thrownError)
            });

    })();
});

My aim here is to catch when the AJAX call cannot make a successful call, or returns a non-parse-able string, or returns an error flag within a successful call (signalling an error from the PHP script). 我的目的是捕获AJAX调用何时无法进行成功​​调用,或者返回无法解析的字符串,或者在成功调用内返回错误标志(表示来自PHP脚本的错误)。

My first problem is that the dialog call just won't fire - no error or warning given. 我的第一个问题是对话框调用不会触发-没有错误或警告。 This is the case even when I tie it to a simple click event. 即使我将其与简单的click事件关联,也是如此。 If anyone can point out where this is failing, I'd be grateful (the wrapping function is being called, and I can pass and echo content to and from it - it just fails at the dialog call). 如果有人指出失败的地方,我将不胜感激(正在调用包装函数,并且我可以向其传递内容或从中回显内容-对话框调用中它只会失败)。

Secondly, can someone clarify for me the difference between the ajax success , done , and complete functions, and between error and fail ( fail does not seem to be recognised by the IDE, although it appears in the docs, but that's probably a separate problem). 其次,有人可以为我澄清ajax successdonecomplete功能之间以及errorfail之间的区别吗(尽管fail出现在文档中,但IDE似乎无法识别fail ,但这可能是一个单独的问题)。

I'm not sure why your dialog isn't displaying, maybe this will work: 我不确定为什么您的对话框没有显示,也许可以用:

$(feedbackWrapper).dialog({
    title: 'An Error Occurred',
    dialogClass: 'feedback-error',
    buttons: [
        {
            'OK' : function() {
                //do stuff
                $(this).dialog('close');
            }
        }
    ]
}).dialog("open");

I'm not sure why this would be needed, since autoOpen: true is the default. 我不确定为什么需autoOpen: true ,因为autoOpen: true是默认设置。 But if you've already closed the dialog from a previous error, I think you need to open it explicitly the next time. 但是,如果您已经从上一个错误中关闭了该对话框,那么我认为您下次需要显式打开它。

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

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