gpt4 book ai didi

javascript - jsonp : Uncaught SyntaxError: Unexpected token <

转载 作者:行者123 更新时间:2023-11-28 07:17:54 30 4
gpt4 key购买 nike

我需要加载一个 aspx 页面,该页面使用 jsonp 返回一个文本字符串,因为它是跨域调用。目标页面将加载到 div 标签内,并在源页面上显示为模式窗口。

我正在使用一个测试表单来传递目标页面期望的3个参数,一个提交按钮,非常简单。

当我提交页面时,我收到一条错误消息:Uncaught SyntaxError: Unexpected token <

当我点击错误时,我看到错误指向这一行:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

这是我第一次使用 json/jsonp,我不知道如何修复这个错误。是否可以寻求一些帮助来解决这个问题?

我的调用页面:

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

<script type="text/javascript">
$(function () {
$("#dialog").dialog({
autoOpen: false,
modal: true,
title: "Details",
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
$("#btnSubmit").click(function () {
$.ajax({
type: "GET",
dataType: "jsonp",
crossDomain: true,
url: "http://localhost:81/order-error.aspx",
contentType: "application/json; charset=utf-8",
data: { 'order': $("#order").val(), 'site': $("#site").val(), 'env': $("#env").val() },
success: function (r) {
$("#dialog").html(r.d);
$("#dialog").dialog("open");
}
});
});
});
</script>
</head>
<body>
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="order" value="" /></td>
<td><input type="text" id="site" value="" /></td>
<td><input type="text" id="env" value="" /></td>
</tr>
<tr>
<td><input type="button" id="btnSubmit" value="Submit" /></td>
</tr>
</table>
<div id="dialog" style="display: none">
</div>
</body>
</html>

我的目标/响应页面(order-error.aspx):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<style type="text/css">
body {
margin: 0;
padding: 0;
font-size: 11px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
#content {
width: 100%;
display: inline-block
}
.label {
position:relative;
float:left;
top:0;
margin-right:5px;
}
</style>
</head>
<body>
<div id="content">
<div class="order">
<div class="label">Web Order#: </div>
<div id="orderno" runat="server" class="value"></div>
</div>
<div class="error">
<div class="label">Message: </div>
<div class="value">
<asp:Label ID="lblOrderError" runat="server" Visible="false"></asp:Label></div>
</div>
</div>
</body>
</html>

以及背后的代码:

Partial Class order_error
Inherits System.Web.UI.Page

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

Dim siteKey As String = String.Empty
Dim orderId As String = String.Empty

siteKey = Request.QueryString("site").Trim
orderId = Request.QueryString("order").Trim

Me.lblOrderError.Text = Functions.GetAXErrorMessage(siteKey, orderId)

Me.orderno.InnerText = orderId.Trim
lblOrderError.Visible = True

End Sub
End Class

order-error.aspx的输出html:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="content">
<div class="order">
<div class="label">Web Order#: </div>
<div id="orderno" class="value">A1G759</div>
</div>
<div class="error">
<div class="label">Message: </div>
<div class="value">
<span id="lblOrderError"><Fault xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Fault">
<Code />
<Reason>
<Text xml:lang="EN-US">User is not authorized for this Endpoint.</Text>
</Reason>
</Fault></span></div>
</div>
</div>

<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Chrome","requestId":"757708942b1d4af892b199f3590d85f5"}
</script>
<script type="text/javascript" src="http://localhost:63737/17a3dfffdc8f48ad9496d260bd296120/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>

最佳答案

该错误显然是由您的 AJAX 调用返回 HTML 而不是 JSON 引起的。

success 处理程序中,您有 $("#dialog").html(r.d);。在这里,您尝试解析对对象的调用 (r) 的输出(r.d 强制执行此操作),但由于明显的原因而失败。

您应该从 AJAX 方法返回 JSON,可能使用 HTTP 处理程序或 WebAPI。

关于javascript - jsonp : Uncaught SyntaxError: Unexpected token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30649225/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com