发布请求以包含“Content-Type”和JSON [英] Post request to include 'Content-Type' and JSON

查看:128
本文介绍了发布请求以包含“Content-Type”和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用goo.gl进行URL缩短。我需要提出以下请求:

I'm to work with goo.gl for URL shortening. I need to make the following request:

POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "http://www.google.com/"}

我的html: -

my html:-

<form method="post" action="https://www.googleapis.com/urlshortener/v1/">
    <button type="submit"> submit </button>
</form>

我如何在这里添加'content-type'和json?

how do i add the 'content-type' and json here?

推荐答案

浏览器不支持JSON作为表单提交的媒体类型(支持的类型是在规范中列出

Browsers do not support JSON as a media type for form submissions (the supported types are listed in the spec).

从网页发出此类请求的唯一方法是使用XMLHttpRequest对象。

The only way to make such a request from a web page is to use the XMLHttpRequest object.

Google提供一个JavaScript库(包装XMLHttpRequest),可以与网址缩短API

Google provide a JavaScript library (which wraps XMLHttpRequest) that can interact with their URL Shortener API.

这篇关于发布请求以包含“Content-Type”和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆