javascript Uncaught SyntaxError: Unexpected token < in <!DOCTYPE html>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28600916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:14:56  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected token < in <!DOCTYPE html>

javascriptjqueryajaxjsonp

提问by Muhammad Saqlain Arif

i am sending a ajax request to external domain. Here is my code, There might be an issue on JSONP response while converting the html data to jsonp. I have tried so many solution because i am requesting to cross domain so i have to use JSONP else i have to face cross domain error. Error when Use simple JSON ERROR: " XMLHttpRequest cannot load http://www.blink.com.kw/search-result.aspx?text=apple&searchfor=all. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:49324' is therefore not allowed access."

我正在向外部域发送 ajax 请求。这是我的代码,将 html 数据转换为 jsonp 时,JSONP 响应可能存在问题。我尝试了很多解决方案,因为我要求跨域,所以我必须使用 JSONP,否则我必须面对跨域错误。使用简单 JSON 错误时出错:“ XMLHttpRequest 无法加载http://www.blink.com.kw/sea​​rch-result.aspx?text=apple&searchfor=all。没有 'Access-Control-Allow-Origin' 标头出现在请求的资源。因此不允许访问源 'localhost:49324'。”

Response error: Uncaught SyntaxError: Unexpected token <

响应错误:未捕获的语法错误:意外的标记 <

<script type="text/javascript">
  $(document).ready(function(){
      $("#bt").click(function(){
       $.ajax({
        type: 'GET',
        url: 'http://www.blink.com.kw/search-result.aspx?text=apple&searchfor=all',
        dataType: 'jsonp',
        success: function (data) {          
        console.log(data);
//$("#data").html(data);
        }
        }); 
    });
});
</script>

回答by Samuel Goodell

This is probably happening because you are specifying it as JSONP, which executes the data as a script in order to execute a callback function. If it sends back a normal HTML document with the doctype being the first line it sees, this would occur.

这可能是因为您将其指定为 JSONP,它将数据作为脚本执行以执行回调函数。如果它发回一个普通的 HTML 文档,文档类型是它看到的第一行,就会发生这种情况。

回答by Bukhbayar

Try this code, basically we should not use url like this. Also, this url is not return any json or jsonp format, please check your link as well

试试这段代码,基本上我们不应该像这样使用url。此外,此网址不返回任何 json 或 jsonp 格式,请检查您的链接

<script type="text/javascript">
  $(document).ready(function(){
      $("#bt").click(function(){
       $.ajax({
        type: 'GET',
        url: 'http://www.blink.com.kw/search-result.aspx',
        dataType: 'jsonp',
        data:{
            text: apple,
            searchfor: all
        }
        success: function (data) {          
        console.log(data);
        }
        }); 
    });
});
</script>

Hope this helps :)

希望这可以帮助 :)