我正在为内部公司应用程序设计一个 Restful Web 服务,并且很好奇如何合并 JSON 和“Web 内容”请求。
与所有优秀的企业应用程序一样,Web 应用程序具有反映在 URL 中的三个字母的缩写。假设该应用程序的“呼号”是 abc,用户通过以下 URL
http://servername/abc
当用户访问应用程序的根目录时,我们希望为他们提供主 html 页面、.js 文件(包括 jquery)、css 和图像。然后,jquery 将开始向服务器发出 AJAX 调用。
处理这些多种内容类型的最佳方法是什么?
http://servername/abc (返回index.html的内容)
http://servername/abc/javascript/jquery.js (返回 js 文件)
http://servername/abc/countries/de(返回 JSON)
我应该将其拆分为两个 Web 上下文吗?我应该在 ajax 调用中使用 jquery contentType 参数来显式指定 JSON、HTML 或其他内容吗?
I am designing a Restful web service for an internal corporate application, and am curious how to merge JSON and "web content" requests.
The web application, like all good corporate applications, has a three-letter abbreviation that is reflected in the URL. Let's say that this application's "call sign" is abc, and the users access it at the following URL
http://servername/abc
When the users access the root of the application we want to serve them up the main html page, the .js files (including jquery), the css, and the images. Then, jquery will start to make AJAX calls back to the server.
What is the best way to handle these multiple content types?
http://servername/abc (returns contents of index.html)
http://servername/abc/javascript/jquery.js (returns a js file)
http://servername/abc/countries/de (returns JSON)
Should I split this into two web contexts? Should I use the jquery contentType parameter in the ajax calls to explicitly specify JSON versus HTML versus something else?
发布评论
评论(2)
jQuery
contentType
实际上只是一个参数,用于在向服务器发出的 HTTP 请求上设置内容类型标头。为 AJAX 调用设置这些始终是最佳实践。额外:
另一个好的做法是指定
dataType
参数,因为它将在 HTTP 请求上设置接受标头。这对于 GET 和 POST AJAX 请求都很有用。大多数(如果不是全部)Web 服务框架(Rails、ASP.NET MVC、.NET WCF 等)都能够检查 HTTP 请求的标头并确定要返回的内容类型
,例如:
HTTP 标头中的
application/json
会让您的 Web 服务知道返回 JSON 响应而不是 HTML 或 XML 响应。我见过的一些为 HTML/JSON 服务组织的 Web 应用程序的更好方法是让标准路由始终为 HTML 页面和资源提供服务,即:
http://servername/abc
http://servername/abc/javascript/jquery。 js
会完全按照你所说的那样做。对于 JSON(甚至 XML)响应,我看到人们创建了一个明确理解的路由来返回这些类型的响应,即:
http://servername/api/abc/countries/de
url 路由以
/api/
开头,它始终被理解为返回非 html JSON/XML 响应这使您的公司可以轻松地内部和外部都了解
/api/
路由是您的 JSON/XML 响应。如果您想要这样做,它还可以更轻松地向外部客户公开这些方法,因为基础设施就在那里,您只需要对请求进行身份验证等。the jQuery
contentType
is really just a parameter that sets your content-type header on your HTTP request to the server. It is always a best practice to set these for your AJAX calls.Added:
Another good practice is to specifiy the
dataType
parameter as that will set the accept header on your HTTP request. This is useful for both GET and POST AJAX requests.Most, if not all, web service frameworks (Rails, ASP.NET MVC, .NET WCF, etc..) have abilities to examine the headers of an HTTP request and determine what type of content to serve back
for example:
application/json
in an HTTP Header would let your webservice know to return a JSON response instead of an HTML or XML response.Some of the better ways that I have seen web apps organized for HTML/JSON serving is to make your standard routes always serve your HTML pages and resources, ie:
http://servername/abc
http://servername/abc/javascript/jquery.js
would do exactly as you have said. For your JSON (or even XML) responses, I see folks create a route that is explicity understood to serve back those types of responses, ie:
http://servername/api/abc/countries/de
the url route begins with an
/api/
which would always be understood to serve back a non-html JSON/XML responsethis makes it pretty easy for your company to internally and externally understand that
/api/
routes are your JSON/XML responses. It also makes it easier to expose these methods to your customers externally should you want to do that as the infrastructure is there, you would just need to authenticate, etc... the requests.一个好方法是使用标准 HTTP 接受标头。在 ajax 请求中,您可以将此标头指定为 application/json,然后所有其他 Web 请求将包含浏览器的接受标头。然后,在服务器端,您可以使用 Accept 标头来确定要提供的内容。
如果您使用 JQuery,则如果您的 dataType 参数设置为“json”,Accept 标头将自动设置为 application/json。
A good way to do this is to use the standard HTTP Accept Header. In your ajax requests, you would specify this header to be application/json, and then all other web requests would include the browser's accept headers. Then, on the server side, you could use the Accept header to determine which content to serve.
If you use JQuery, then the Accept header will be set to application/json automatically if your dataType parameter is set to "json."