简体   繁体   English

Ajax错误:SyntaxError:意外的令牌<错误

[英]Ajax Error: SyntaxError: Unexpected token < error

So I have searched the forum for similar questions unfortunately I have been unlucky to find an answer that suits my problem. 因此,很遗憾,我在论坛上搜索了类似的问题,但我很不幸找不到适合我问题的答案。 I am trying to make an ajax call in my jquery using the following code; 我正在尝试使用以下代码在我的jquery中进行ajax调用;

function submitForm(formData)
    {
    $.ajax({
        type: 'POST',
        url: 'addEvents.php',
        data: formData,
        dataType:'json',
        cache:false,
        timeout:7000,

        success: function(data){
            $('#addEventsForm #response').removeClass().addClass((data.error === true) ? 'error' : 'success').html(data.msg).fadeIn('fast');

            if($('#addEventsForm #response').hasClass('success')){
                setTimeout("$('#addEventsForm')",5000);
            }

        },

        error:function(XHR,textStatus,errorThrown)
        {
            $('#addEventsForm #response').removeClass().addClass('error').html('<p> There was an <strong>' + errorThrown +
                '</strong> error due to <strong>'+ textStatus +'</strong> condition.</p>').fadeIn('fast');
            console.log(arguments);
        //alert(XMLHttpRequest.responseText);
        },

        complete:function(XHR,status)
        {
            $('#addEventsForm')[0].reset();
        }
    });
}

But I am getting a sysntax error. 但是我收到了sysntax错误。 I have tried doing this to see what errors I get in chrome 我尝试这样做是为了查看chrome中出现的错误

console.log(arguments);

but the responseText in the [Object, "parsererror" , SyntaxError] node seems to display the entire html of the page that contains the form where I am inserting the record. 但是[Object, "parsererror" , SyntaxError]节点中的responseText似乎显示了包含要在其中插入记录的表单的页面的整个html。

I am still getting my feet wet in ajax so I am not yet a pro in absolutely understanding the error messages and what they mean. 我仍然在用ajax弄湿我的脚,所以我还不是绝对了解错误消息及其含义的专家。

Is the above js/jQuery code located on the addEvents.php page -- that is, on same page that you are posting the AJAX to? 上面的js / jQuery代码是否位于addEvents.php页面上,也就是将AJAX发布到的页面上? (In other words, are both the form/ajax and the AJAX destination on the same physical PHP page?) (换句话说,表单/ ajax和AJAX目标都在同一物理PHP页面上吗?)

If so, you will get exactly what you describe: the entire HTML of the page spat back at you. 如果是这样,您将完全得到您所描述的内容:页面的整个HTML都向您吐口水。

AJAX, by design, must post to a different physical page on the server. 根据设计,AJAX必须发布到服务器上的其他物理页面。 That's just how it works. 这就是它的工作原理。

Also, FWIW, note that dataType: 'json', refers to the data being returned, not the data being sent. 同样,FWIW,请注意dataType: 'json',指的是返回的数据,而不是发送的数据。 Perhaps you already know this, but it is a common misunderstanding so is worth mentioning. 也许您已经知道这一点,但这是一个普遍的误解,因此值得一提。

Edit: 编辑:

As Felix points out, this answer is not technically perfect. 正如Felix指出的那样,这个答案在技术上并不完美。 It is possible to post AJAX code to the same page, but you must specifically allow for that. 可以将AJAX代码发布到同一页面,但是您必须特别允许这样做。 Perhaps FK or another could edit this post to add an example of what that additional code would look like? 也许FK或其他人可以编辑这篇文章,以添加示例示例,说明这些附加代码是什么样的?

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

相关问题 $ .ajax错误未捕获SyntaxError:意外令牌: - $.ajax error uncaught SyntaxError: Unexpected token : SyntaxError: Unexpected token &lt; in JSON at position 0&quot; 错误在 AJAX - SyntaxError: Unexpected token < in JSON at position 0” error in AJAX Ajax:其余api调用给出错误:未捕获的SyntaxError:意外的令牌: - Ajax: rest api call give error: Uncaught SyntaxError: Unexpected token : Rails Ajax错误-SyntaxError:JSON中位置0处的意外令牌C - Rails ajax error - SyntaxError: Unexpected token C in JSON at position 0 AJAX发布错误-SyntaxError:JSON位置5出现意外的令牌R - AJAX Post Error - SyntaxError: Unexpected token R in JSON at position 5 jQuery ajax Long Polling追加错误parsererror(SyntaxError:意外令牌&lt;) - jquery ajax Long Polling append error parsererror (SyntaxError: Unexpected token <) “语法错误:意外的标记 &#39;:&#39;。解析错误。” JSON 和 ajax - "SyntaxError: Unexpected token ':'. Parse error." JSON & ajax javascript错误“ SyntaxError:意外令牌&amp;&amp;” - javascript error “SyntaxError: Unexpected token &&” 错误未捕获的语法错误:意外令牌{ - Error Uncaught SyntaxError: Unexpected token { “未捕获的SyntaxError:意外的令牌(”但没有错误 - “Uncaught SyntaxError: Unexpected token (” but there is no error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM