简体   繁体   中英

Ajax JSON response undefined in prompt field

I don't know what is wrong with my code. I tried to fill javascript prompt with ajax response. Here is my code :

        $.ajax({
            url: "{{ url('/dashboard/popfieldexist') }}",
            dataType: "json",
            type: 'POST',
            data: "_token={{ csrf_token() }}&"+"selector="+f1[0].getAttribute('data-file'),
            success: function (response) {
                if(response){
                    getPopfield = response.data.field;
                }else{
                    getPopfield = " ";
                }
            }
        });
        var title = prompt("File name : ", ""+getPopfield+"");

未定义

it display undefined..

An ajax call is usually asynchronous. So your prompt is called before the ajax finished.

Try:

   $.ajax({
        url: "{{ url('/dashboard/popfieldexist') }}",
        dataType: "json",
        type: 'POST',
        data: "_token={{ csrf_token() }}&"+"selector="+f1[0].getAttribute('data-file'),
        success: function (response) {
            if(response){
                getPopfield = response.data.field;
            }else{
                getPopfield = " ";
            }
            var title = prompt("File name : ", ""+getPopfield+"");
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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