简体   繁体   English

jQuery Mobile AJAX无法加载页面

[英]jQuery Mobile AJAX doesn't load page

echo '<script>
    function ' . $row['idname'] . 'Click(){
        $( "#flip-' . $row['idname'] . '" ).flipswitch( "disable" );
        var isOff = document.getElementById("flip-' . $row['idname'] . '").value;
        if(isOff == "off"){
            console.log("action.php?do=turn-off-' . $row["id"] . '");
            jQuery.ajax({
                url: "action.php?do=turn-off-' . $row["id"] . '",
                type: "GET",
                success:function (data) {
                    $( "#flip-' . $row['idname'] . '" ).flipswitch( "enable" );
                    console.log("SUCCESS!!");
                }
            });
        }else{
            console.log("action.php?do=turn-on-' . $row["id"] . '");
            jQuery.ajax({
                url: "action.php?do=turn-on-' . $row["id"] . ',
                type: "GET",
                success : function (data) {
                    $( "#flip-' . $row['idname'] . '" ).flipswitch( "enable" );
                    console.log("SUCCESS!!");
                }
            });
        }
    }
    </script><br>';

So my problem is that ajax doesn't even load the page that is requested. 所以我的问题是ajax甚至没有加载请求的页面。

It creates this code from the result of a MySQL query, so it's basically a JavaScript file in a PHP File. 它根据MySQL查询的结果创建此代码,因此它基本上是PHP文件中的JavaScript文件。 The target is an empty page that only has some functions. 目标是一个仅具有某些功能的空白页。

The function is invoked onChange from this <select> : 从此<select> onChange调用该函数:

echo '<div class="ui-field-contain" style="width: 100%;">
    <label for="flip-' . $row['idname'] . '">' . $row['name'] . '</label> 
    <select onchange="' . $row['idname'] . 'Click()" name="flip-' . $row['idname'] . '" id="flip-' . $row['idname'] . '" data-role="flipswitch">
        <option value="off" selected="">Aus</option> <option value="on">An</option> 
    </select>
 </div>';

This may solve the problem because it is not a nice way to just replace it with always. 这可能解决了问题,因为这不是始终替换它的好方法。 You might consider adding some error handling where no wrong elements are tested to be selected with the error message as a part of the id. 您可能会考虑添加一些错误处理,其中不会测试任何错误的元素,并且将错误消息作为ID的一部分。

Deprecated jqXHR.success() 不推荐使用jqXHR.success()

The issue may occur because this method is deprecated since jQuery 1.8. 因为自jQuery 1.8起不推荐使用此方法,所以可能会出现此问题。 It has been replaced by jqXHR.done(). 它已被jqXHR.done()取代。

jQuery.ajax({
    url: "action.php?do=turn-on-' . $row["id"] . '",
    type: "GET",
    done: function (data) {
        $( "#flip-' . $row['idname'] . '" ).flipswitch( "enable" );
        console.log("SUCCESS!!");
    }
});

Suggesting another way 建议另一种方式

$.get("api.php", {
    do: "turn-on-' . $row["id"] . '"
}, function (data) {
    console.log(data);}
});

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

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