jQuery Ajax请求语法错误:意外令牌< [英] jquery Ajax Request SyntaxError: Unexpected token <

查看:76
本文介绍了jQuery Ajax请求语法错误:意外令牌<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用get请求返回一系列产品.响应返回带有200请求的XML.

I am trying to return an array of products using a get request. The response returns XML with a 200 request.

Web服务:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public List<product> GetAllProducts()
{
    using (SchulteDesignYourOwnEntities db = new SchulteDesignYourOwnEntities())
    {
        return db.products.ToList();
    }
}

这是我的代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title></title>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {

                $.ajax({
                    url: 'http://www.organizeliving.com/designwebservice.asmx/GetAllProducts',
                    dataType: 'json',
                    success: function (result) {
                        alert("Result: " + result.length);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        console.log("Status: " + xhr.status);
                        console.log("Message: " + thrownError);
                    }
                });


            });
        </script>
    </head>

    <body></body>

</html>

推荐答案

您将dataType作为'json'. jQuery将自动尝试从响应中解析JSON.如果不能,则认为它是错误.

You have the dataType as 'json'. jQuery will automatically try to parse JSON from the response. If it cannot, it considers it an error.

XML不是有效的JSON(它实际上会讨厌开头<).您可以将dataType更改为'xml'(或不进行任何更改),或者实际上从服务器发出纯JSON.

XML is not valid JSON (it will really hate the opening <). You can either change the dataType to 'xml' (or nothing) or actually emit pure JSON from the server instead.

这篇关于jQuery Ajax请求语法错误:意外令牌&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆