Ajax problems encountered in the operation

ajax jquery is used in performing the function operation success after sending the request to the back,

 $('#btna').on('click', function () {
            var a = $(".check");
            var roomlist = [];
            a.each(
                function (index) {
                    roomlist.push(a[index].getAttribute('dataid'));
                }
            );
            $.ajax({
                url: "{:url('admin/Main/btna')}",
                type: "post",
                data: {
                    'roomid': JSON.stringify(roomlist)
                },
                dataType: 'json',
                success: function (res) {
                    window.location.reload();
                }
            });
        })

This is js operating a button click event execution, after the request is successful, just want to achieve automatic page refresh, wimdow.location.reload (), but there is a problem, there has been no effect,

Php part of the code is such that

    public function btna(Request $request)
    {
        // $this->islogin();
        $roomid = $request->param('roomid');

        $a = json_decode($roomid, true);

        foreach ($a as $k => $v) {
            $res = Db::table('room')->where('room_id', $v)->update(['static' => '1', 'color' => 'Blue ' ]); 
        } 

        // IF ($ RES> 0) {
         //      $ MSG = [
         //          ' Status' => 0,
         //          'MSG' => 'modified successfully',
         //          'roomId' => roomId $
         //      ];
         // } the else {
         //      $ MSG = [
         //          'Status' =>. 1,
         //          'MSG' => 'modify unsuccessful',
         //          'roomId' => $ roomId
         //      ];
         // }
         // return $ MSG;
    }

After this time if judged to have been commented out, then the question arises, why not give back the return value, and the js does not need to receive any value, auto-refresh function is not performed?

There are two solutions:

1. If you say that the value of the background to execute code, does not return any value to ajax, then we need to dataType: 'json' this deletion, the auto refresh function can be achieved

2. Add the background say ajax need to return a value, you need to let go of the comment, so this code is executed, the front desk does not require changes to ajax part, the same operations can also be performed in the success, the key is this normal

Not noticed dataType: 'json',

 dataType: 'json', expected data type returned by the server, if not specified, jQuery will automatically be determined according to intelligent HTTP MIME information packet, such as the XML MIME type was identified as XML. In 1.4, JSON will generate a JavaScript object, and the script will execute this script. Then the server returns the data based on the value resolution, passed to the callback function. Available values:
"xml": returns an XML document, available jQuery process.
"html": Returns the HTML Plain text information; script tag contains will be executed when inserting dom.
"script": return plain text JavaScript code. It does not automatically cached results. Unless the "cache" parameter sets. '' 'Note:' '' in the remote request (not in the same domain under), all POST requests will be converted to a GET request. (Because of the use of DOM script tag to load)
"json": return JSON data.
"jsonp": JSONP format. When using JSONP form of calling functions, such as "myurl? Callback =?" JQuery automatically replaces the? Correct function name to execute the callback function.
"text": return plain text string

Guess you like

Origin www.cnblogs.com/dumenglong/p/11456890.html