如何使一个角JSONP POST请求? [英] How to make a JSONP POST request in angular?

查看:119
本文介绍了如何使一个角JSONP POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方文档中描述的$ http.jsonp方法似乎始终执行get请求:<一href=\"http://docs.angularjs.org/api/ng\">http://docs.angularjs.org/api/ng.$http#methods_jsonp.

The $http.jsonp method described in the official documentation seems to always perform get requests: http://docs.angularjs.org/api/ng.$http#methods_jsonp.

我曾尝试配置选项设置为POST,但它仍然发送GET:

I have tried setting the config option to 'POST' but it still sends a GET:

$http.jsonp('/api/new?callback=JSON_CALLBACK', {method: 'POST'});

我也试过,希望将角度切换到岗位设置一个数据参数:

I have also tried setting a data argument in the hope that angular would switch to a POST:

$http.jsonp('/api/new?callback=JSON_CALLBACK', {data: {stuff: true}});

但它仍然没有:)

But it still doesn't :)

作为制作后是这样的:

$http.post('/api/new?callback=JSON_CALLBACK')

这确实让POST明显,但不会做魔角啄与JSON_CALLBACK并产生以下JS错误:

It does make a POST obviously but doesn't do the angular magic thingy with the JSON_CALLBACK and produces the following JS error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xxx.yyy.zzz' is therefore not allowed access.

(API是不相同的服务器上的应用程序,这是JSONP点)​​。

(The API is not on the same server as the app, that's the point of JSONP).

谷歌是最无助于这一问题,并通过角的源读取是不是最简单的任务。那么,如何可以与一个角JSONP POST请求?

Google was most unhelpful on this issue and reading through angular's sources is not the easiest task. So how can I make a JSONP POST request with angular?

推荐答案

您无法通过JSON-P(带或不带角)POST请求

You cannot make a POST request using JSON-P (with or without Angular)

一个JSON-P请求的工作原理是生成一个&LT;脚本&GT; 的src 属性元素。这将始终触发一个GET请求。

A JSON-P request works by generating a <script> element with a src attribute. This will always trigger a GET request.

如果你想使用JavaScript跨域POST请求,那么你必须使用XMLHtt prequest(和在服务器电源适合门禁头按的的CORS规范)或通过代理服务器的请求托管的页面。

If you want to make a cross-domain POST request with JavaScript then you must use either XMLHttpRequest (and have the server supply suitable access control headers as per the CORS specification) or proxy the request through the server hosting the page.

这篇关于如何使一个角JSONP POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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