使用 C# asp.net 打印 json
好的,我有一些 jQuery 代码可以将 AJAX 请求发送到 aspx 文件。
我的 Spellchecker.aspx 文件如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Spellchecker.aspx.cs" Inherits="Spellchecker" %>
<head id="Head1" runat="server" />
我必须放置此“head”标签,否则我会在 web.config 文件中收到有关“<页面主题”的错误(网站中的其他页面需要该文件)。这意味着来自服务器的响应采用以下形式:
在 aspx.cs 文件中,我在 Page_Load 中返回一个转换为 json 的字典:
dict.Add("just_json", json_obj);
JavaScriptSerializer serializer = new JavaScriptSerializer(); //creating serializer instance of JavaScriptSerializer class
string json = serializer.Serialize((object)dict);
Response.Write(json);
}
因此在警报框中,我看到 json 数据,后跟
如何才能只从 aspx 返回 JSON 数据?
更新:我想我已经弄清楚了。将 Theme="" 放在 aspx 文件中的“% Page”标记中似乎禁用主题!
Alright, so I have some jQuery code that will send an AJAX request to an aspx file.
My Spellchecker.aspx file looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Spellchecker.aspx.cs" Inherits="Spellchecker" %>
<head id="Head1" runat="server" />
I had to put this "head" tag, otherwise I get an error regarding "<page theme" in web.config file (which I need for other pages in the site). This means that the response from server comes in the form of: <JSON HERE><head .../> which is wrong because the code should just return json data.
In the aspx.cs file, I am returning a dictionary-converted-to-json in Page_Load:
dict.Add("just_json", json_obj);
JavaScriptSerializer serializer = new JavaScriptSerializer(); //creating serializer instance of JavaScriptSerializer class
string json = serializer.Serialize((object)dict);
Response.Write(json);
}
So in an alert box, I see json data, followed by <head id="Head1"><link href..." stylesheets etc.
How can I make it so only JSON data is returned from aspx?
UPDATE: I think I figured this out. Putting Theme="" in the "% Page" tag in the aspx file seems to disable the theme!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答您的实际问题“为什么我需要 head 元素” - 因为这是 ASP.NET 放置 CSS 链接和一些 JavaScript 导入的地方。
目前还不清楚您到底想在这里做什么,但看起来您可能想要创建一个 Web 服务或将一个方法公开为 ScriptMethod。使用 ASPX 页面来输出对 AJAX 请求的响应是很奇怪的。
查看 ScriptMethods 或 HttpHandlers。
HttpHandler 允许您完全管理响应。因此,您将创建一个处理程序并将其挂钩到“SpellChecker.ashx”,并且该处理程序可以直接写入响应流。
然后,在 system.webServer 元素内的 Web.Config 中添加:
现在您可以向处理程序发出请求,例如
http://localhost/SpellChecker.ashx?TextToCheck=xyz
。To answer your actual question of "why do I need the head element" - because this is where ASP.NET puts your links to CSS and some JavaScript imports.
It's unclear exactly what you're trying to do here but it looks like you probably want to create a Web Service or expose a method as a ScriptMethod. It is strange to use an ASPX page for the purpose of outputting a response to an AJAX request.
Look into ScriptMethods or HttpHandlers.
HttpHandlers allow you to manage the response completely. So you would create a handler and hook it to "SpellChecker.ashx" and the handler could write directly to the response stream.
And then, in your Web.Config inside the system.webServer element, add:
Now you can make a request to your handler like
http://localhost/SpellChecker.ashx?TextToCheck=xyz
.如果您的页面所做的只是处理输入和输出 JSON,请考虑使用以 ashx 结尾的“通用处理程序”页面,而不是以 asmx 结尾的“网页”。它的开销要少得多,并且不会尝试加载主题等。
您可以通过控制输出的 Content-Type 来让它输出 JSON 而不是 XML 或其他内容:
If all your page is doing is processing input and outputting JSON, consider using a "Generic Handler" page that ends in ashx instead of a "Web Page" that ends in asmx. It has a lot less over head, and won't try loading themes, etc.
You can get it to output JSON instead of XML or something else by controlling the Content-Type of the output: