jquery Ajax Request SyntaxError:意外的令牌<

时间:2012-12-05 15:28:29

标签: jquery ajax asmx

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

网络服务:

[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>

1 个答案:

答案 0 :(得分:14)

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

XML是无效的JSON(它真的很讨厌开放<)。您可以将dataType更改为'xml'(或不执行任何操作),或者实际从服务器发出纯JSON。

相关问题