Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

jquery - jQuery函数响应未定义(Jquery function response is undefined)

I've been working on a project and I recently came across this strange Jquery behaviour!

(我一直在从事一个项目,最近遇到了这种奇怪的Jquery行为!)

Code Snippet

(代码段)

$("#form_1").submit(function (event){
    event.preventDefault();
    response =  sendform('form_1', '../php/signup-01.php', 'POST', '');
    console.log(response.responseText);
});

sendform()

(sendform())

function sendform(id, location, method, extrapars) {
    response = $.ajax({
        url : location ,
        data : $("#"+id).serialize() + extrapars ,
        cache : false ,
        processData : false  ,
        type : method ,
        success : function (successmsg) {
            return successmsg;

                }
            });
    return response;
}

So basically I'm trying to send some form data through a pre-defined function, but when I try to log it response in my console it says Undefined .

(因此,基本上,我试图通过预定义的函数发送一些表单数据,但是当我尝试在控制台中记录响应时,它显示为Undefined 。)

I know that this is an JS Object, so I even tried using Console.log(response[responseText]) .

(我知道这是一个JS对象,所以我什至尝试使用Console.log(response[responseText]) 。)

When I try to log the complete object, Console.log(response) , Its something LIKE

(当我尝试记录完整的对象Console.log(response) ,有点像)

abort: ? ( statusText )
always: ? ()
catch: ? ( fn )
done: ? ()
fail: ? ()
getAllResponseHeaders: ? ()
getResponseHeader: ? ( key )
overrideMimeType: ? ( type )
pipe: ? ( /* fnDone, fnFail, fnProgress */ )
progress: ? ()
promise: ? ( obj )
readyState: 4
responseText: "Unused OTP in DB!"
setRequestHeader: ? ( name, value )
state: ? ()
status: 200
statusCode: ? ( map )
statusText: "OK"
then: ? ( onFulfilled, onRejected, onProgress )
__proto__: Object

So any help with that would be helpful for me.

(因此,对此的任何帮助将对我有所帮助。)

Thanks in advance!

(提前致谢!)

  ask by Arag Aggrawal translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try something like this.

(您可以尝试这样的事情。)

Indifferent to how the $.ajax method's result will be processed, you can further rely on JQuery, so you can forget callbacks.

(不管$.ajax方法的结果如何处理,您都可以进一步依赖JQuery,因此可以忽略回调。)

function sendform(id, location, method, extrapars) {
    return $.ajax({
        url : location ,
        data : $("#"+id).serialize() + extrapars ,
        cache : false ,
        processData : false  ,
        type : method
    });
}

Now, as for the usage, you can either rely on the new fashioned async/await :

(现在,关于用法,您可以依靠新的老式async/await :)

$("#form_1").submit(asnyc function (event){
    event.preventDefault();
    try {
      const response =  await sendform('form_1', '../php/signup-01.php', 'POST', '');
      console.log(response);
   } catch(ex) {
      // handle error
   }
});

Or you can use the JQquery way:

(或者,您可以使用JQquery方式:)

$("#form_1").submit(function (event){
    event.preventDefault();
      sendform('form_1', '../php/signup-01.php', 'POST', '')
      .done(function(data, textStatus, jqXHR) {
        console.log(data);
      })
      .fail(function(jqXHR, textStatus, errorThrown) {
       // handle error      
      });      
});

Deferred is promises compatible, thus they are then -able also.

(递延兼容的承诺,因此它们then -able也。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...