Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在Angular 2中发出http POST请求_Angularjs_Rest_Http Post_Angular - Fatal编程技术网

Angularjs 在Angular 2中发出http POST请求

Angularjs 在Angular 2中发出http POST请求,angularjs,rest,http-post,angular,Angularjs,Rest,Http Post,Angular,我想在Angular 2中提出以下POST请求 http://gearman.local.eviry.com/info 正文如下 {"gearman_servers" :[{ "name" : "server 1", "addr" : "192.168.1.115" }, { "name" : "Server 2", "addr" : "192.0.1.126" } ]} 我已经确定这在Angular 2之外有效。(使用) 我

我想在Angular 2中提出以下POST请求

http://gearman.local.eviry.com/info
正文如下

{"gearman_servers" :[{

    "name" : "server 1",
    "addr" : "192.168.1.115"
    },
    {
    "name" : "Server 2",
    "addr" : "192.0.1.126"
    }
    ]}
我已经确定这在Angular 2之外有效。(使用) 我还确保启用了CORS

下面是我的角度代码

var url = 'http://gearman.local.eviry.com' +
            '/info';
var GEARMAN_DATA = {"gearman_servers" :[{
            "name" : "server 1",
            "addr" : "192.168.1.115"
        },
            {
                "name" : "Server 2",
                "addr" : "192.0.1.126"
            }]};

        let body = JSON.stringify(GEARMAN_DATA);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        this.http.post(url, body, options)
            .map(res =>  res.json())
            .subscribe(
                data => {console.log(data);},
                err => this.logError(err),
                () => console.log('Fetching complete for Server Metrics')
            );

logError(err:Response){
        console.log("some error");
        console.log(err);
    }
然而,这样做给了我一个错误

XMLHttpRequest cannot load http://gearman.local.eviry.com/info. Invalid HTTP status code 405
此外,当我打印出
logError
函数中的错误时,我得到以下信息:

很难看出这个请求有什么问题

请求头如图所示

 Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:gearman.local.eviry.com
Origin:http://evil.com/
Pragma:no-cache
Referer:http://localhost:3000/gearman-ui/job-queue
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36
X-FirePHP:0.4.4
X-FirePHP-Version:0.4.4
X-Wf-Max-Combined-Size:262144
Allow:POST
Cache-Control:no-cache
Connection:Keep-Alive
Content-Length:4519
Content-Type:text/html; charset=UTF-8
Date:Mon, 15 Feb 2016 08:32:54 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.24 (Unix) mod_ssl/2.2.24 OpenSSL/1.0.1e-fips
响应头如图所示

 Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:gearman.local.eviry.com
Origin:http://evil.com/
Pragma:no-cache
Referer:http://localhost:3000/gearman-ui/job-queue
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36
X-FirePHP:0.4.4
X-FirePHP-Version:0.4.4
X-Wf-Max-Combined-Size:262144
Allow:POST
Cache-Control:no-cache
Connection:Keep-Alive
Content-Length:4519
Content-Type:text/html; charset=UTF-8
Date:Mon, 15 Feb 2016 08:32:54 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.24 (Unix) mod_ssl/2.2.24 OpenSSL/1.0.1e-fips
如果有人使用,我有下面的代码。我必须使用angular实现相同的功能。(请注意,您将无法访问
http://gearman.local.eviry.com


我尝试了一下您的请求(请参阅此plunkr:),似乎您预飞行请求的响应不正确:

XMLHttpRequest cannot load http://gearman.local.eviry.com/info. Response for preflight is invalid (redirect)
看了这封信的回复后,我有以下几点:

  • 概述

    Request URL:http://gearman.local.eviry.com/info
    Request Method:OPTIONS
    Status Code:302 Found
    Remote Address:49.212.179.100:80
    
  • 响应标题

    Connection:close
    Content-Length:293
    Content-Type:text/html; charset=iso-8859-1
    Date:Mon, 15 Feb 2016 08:12:44 GMT
    Location:https://gearman.local.eviry.com/info
    Server:Apache
    
    Accept:*/*
    Accept-Encoding:gzip, deflate, sdch
    Accept-Language:en-US,en;q=0.8,fr-FR;q=0.6,fr;q=0.4
    Access-Control-Request-Headers:content-type
    Access-Control-Request-Method:POST
    Cache-Control:no-cache
    Connection:keep-alive
    Host:gearman.local.eviry.com
    Origin:http://run.plnkr.co
    Pragma:no-cache
    Referer:http://run.plnkr.co/IwOhToeVuXn1o1a4/
    User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36
    
  • 请求标题

    Connection:close
    Content-Length:293
    Content-Type:text/html; charset=iso-8859-1
    Date:Mon, 15 Feb 2016 08:12:44 GMT
    Location:https://gearman.local.eviry.com/info
    Server:Apache
    
    Accept:*/*
    Accept-Encoding:gzip, deflate, sdch
    Accept-Language:en-US,en;q=0.8,fr-FR;q=0.6,fr;q=0.4
    Access-Control-Request-Headers:content-type
    Access-Control-Request-Method:POST
    Cache-Control:no-cache
    Connection:keep-alive
    Host:gearman.local.eviry.com
    Origin:http://run.plnkr.co
    Pragma:no-cache
    Referer:http://run.plnkr.co/IwOhToeVuXn1o1a4/
    User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36
    

我认为这个请求的302状态码就是问题所在。此外,我在其响应中没有看到任何CORS头…

您能显示您的请求和响应头吗?(开发工具>网络>[选择您的请求]>标题)添加了
httppost的请求标题。。。谢谢你的回复。实际上,
gearman.local.eviry.com
仅在我的本地设置中可用。是的,CORS头不存在,出于测试目的,我使用插件在Chrome上启用了jus。好的,我明白了。您的请求是跨域的吗?您是否有在目标文件之前执行的预发文件(
选项
)?