我将与 goo.gl 合作进行 URL 缩短。我需要提出以下要求:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "http://www.google.com/"}
我的 html:-
<form method="post" action="https://www.googleapis.com/urlshortener/v1/">
<button type="submit"> submit </button>
</form>
如何在此处添加“内容类型”和 json?
最佳答案
浏览器不支持 JSON 作为表单提交的媒体类型(支持的类型是 listed in the spec )。
从网页发出此类请求的唯一方法是使用 XMLHttpRequest 对象。
Google 提供 a JavaScript library (包装 XMLHttpRequest)可以与其交互 URL Shortener API .
关于html - 发布请求以包含 'Content-Type' 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19446544/