【问题标题】:when i want to send data with ajax to php weird error is showing [duplicate]当我想用 ajax 向 php 发送数据时,出现奇怪的错误 [重复]
【发布时间】:2021-01-16 21:42:25
【问题描述】:

当我想使用 ajax 提交表单数据时,错误处理程序没有显示错误。它只是回应这个:

{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}abort: ƒ (e)always: ƒ ()arguments: (...)caller: (...)length: 0name: "always"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: jquery.min.js:2[[Scopes]]: Scopes[3]catch: ƒ (e)done: ƒ ()fail: ƒ ()getAllResponseHeaders: ƒ ()getResponseHeader: ƒ (e)overrideMimeType: ƒ (e)arguments: (...)caller: (...)length: 1name: "overrideMimeType"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: jquery.min.js:2[[Scopes]]: Scopes[3]pipe: ƒ ()progress: ƒ ()promise: ƒ (e)readyState: 4responseText: "<script>console.log('gefunden');</script>
↵<!DOCTY"setRequestHeader: ƒ (e,t)arguments: (...)caller: (...)length: 2name: "setRequestHeader"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: jquery.min.js:2[[Scopes]]: Scopes[3]state: ƒ ()status: 200statusCode: ƒ (e)arguments: (...)caller: (...)length: 1name: "statusCode"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: jquery.min.js:2[[Scopes]]: Scopes[3]statusText: "parsererror"then: ƒ (t,n,r)arguments: (...)caller: (...)length: 3name: "then"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: jquery.min.js:2[[Scopes]]: Scopes[3]__proto__: Object

js/ajax 代码:

let form = $("#form");

   $("#form").on("submit", function(e) {
       e.preventDefault();
                          
       $.ajax({
         url: "file.php",
         method: "POST",
         dataType: 'json',
         data: {
         test: 1
         },
         success: function (r) {
           console.log("bla");
         },
         error: function(data) {
           console.log(data);
         }
       });
    });

【问题讨论】:

    标签: javascript php html jquery ajax


    【解决方案1】:

    error: 函数的第一个参数是jqXHR 对象,而不是错误消息。错误消息在第三个参数中。所以应该是:

    error: function(jqXHR, textStatus, errorMessage) {
        console.log(errorMessage);
    }
    

    【讨论】:

    • 刚刚出现这个错误:SyntaxError: Unexpected token
    • file.php 在 JSON 之前发送 HTML。
    • 我一直在努力寻找解决方案。我该如何解决这个问题?
    猜你喜欢
    • 2015-10-27
    • 2015-03-27
    • 1970-01-01
    • 2018-05-03
    • 2014-11-26
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多