首页 \ 问答 \ Angular http.post作为Options方法请求(Angular http.post as Options method request)

Angular http.post作为Options方法请求(Angular http.post as Options method request)

我正在尝试将一些数据发布到节点服务器,但我有一个问题:

postEmployeeForm(employee: Employee):Observable<any> {
        let body = JSON.stringify(employee);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        return this.http.post('http://localhost:3100/postemployee', body, options)
             .map(this.extractData)
             .catch(this.handleError);
    }

这是我在网络选项卡中的帖子请求我在服务器上看到这是选项方法我期待一个帖子请求。

如果我从http.post中省略选项,那么我在服务器上获得发布请求,但是我有关于意外内容类型的错误。

postEmployeeForm(employee: Employee):Observable<any> {
        let body = JSON.stringify(employee);

        return this.http.post('http://localhost:3100/postemployee', body)
             .map(this.extractData)
             .catch(this.handleError);
    }

这是预期的行为还是我在帖子上做错了。 我在官方角度指南上找到了这个代码: 服务器通信如何处理这种情况,我是否需要在客户端更改某些内容或者我应该在服务器端处理它?

编辑:

这是我的服务器代码:

var http = require('http');
var formidable = require("formidable");
var util = require('util');

var server = http.createServer(function(req, res) {
    res.setHeader('Access-Control-Allow-Origin','*');
    res.setHeader('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept');

    if(req.method.toLowerCase() == 'post') {
        console.log("Get request!");
        processForm(req,res);
        return;
    }
});

function processForm(req,res) {

    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields) {

        res.writeHead(200, {
            'content-type': 'application/json'
        });

        res.end(util.inspect({
            fields: fields
        }));

        console.log('posted fields:\n');
        console.log(util.inspect({
            fields: fields
        }));
    });
}

var port = 3100;
server.listen(port,'localhost');
console.log("server listening on port " + port);

I'm trying to post some data to node server, but I have an issue:

postEmployeeForm(employee: Employee):Observable<any> {
        let body = JSON.stringify(employee);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        return this.http.post('http://localhost:3100/postemployee', body, options)
             .map(this.extractData)
             .catch(this.handleError);
    }

This is my post request by in Network tab I see this as Options method while on server I'm expecting a post request.

If I omit options from http.post then I get post request on server, but then I have error about unexpected content type.

postEmployeeForm(employee: Employee):Observable<any> {
        let body = JSON.stringify(employee);

        return this.http.post('http://localhost:3100/postemployee', body)
             .map(this.extractData)
             .catch(this.handleError);
    }

Is this expected behaviour or I'm doing something wrong with post. I found this code on official angular guide: server-communication How to handle this situation, do I need to change something on client side or should I handle that on server side?

EDIT:

This is my server code:

var http = require('http');
var formidable = require("formidable");
var util = require('util');

var server = http.createServer(function(req, res) {
    res.setHeader('Access-Control-Allow-Origin','*');
    res.setHeader('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept');

    if(req.method.toLowerCase() == 'post') {
        console.log("Get request!");
        processForm(req,res);
        return;
    }
});

function processForm(req,res) {

    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields) {

        res.writeHead(200, {
            'content-type': 'application/json'
        });

        res.end(util.inspect({
            fields: fields
        }));

        console.log('posted fields:\n');
        console.log(util.inspect({
            fields: fields
        }));
    });
}

var port = 3100;
server.listen(port,'localhost');
console.log("server listening on port " + port);

原文:https://stackoverflow.com/questions/41784402
更新时间:2023-08-24 12:08

最满意答案

我不确定我完全理解Hangfire的范围,但这听起来像我使用Quartz Scheduler解决的同类任务


I'm not sure I understand completely the scope of Hangfire, but it sounds like the same kind of tasks I have solved using Quartz Scheduler

相关问答

更多
  • 我问了一个类似的问题 ,但是尽可能地尝试复制.NET框架中的名称,我在Java和Android框架中寻找想法。 似乎Helper , Manager和Util是您不可避免的名词,用于协调不包含状态的类,通常是程序性和静态性的。 一个替代方案是Coordinator 。 你可以得到特别的紫色prosey的名字,并去像Minder , Overseer , Supervisor , Administrator和Master ,但正如我所说,我喜欢保持它像您使用的框架名称。 一些其他常见的后缀(如果这是正确的术语 ...
  • 您需要在类路径中包含相应的jar。 http://eclipse.org/rtp/downloads/如果我是对的...... You need to have corresponding jars in your class path. http://eclipse.org/rtp/downloads/ if i'm correct...
  • 我找了一会儿,但是找不到类似于JFace实现的首选项管理器。 所以,我最终创建了自己的库,最初部分实现了JFace首选项库中的接口,但是因为已经偏离了。 该库名为jpreferences ,托管在Github上。 编辑:去图,当我尝试创建一个名为jpreferences的SourceForge项目时,已经有一个名称的项目。 如果我只想到搜索jpreferences而不是首选项管理器 。 哦,现在任何寻找我所寻找的东西的人都有更多的选择。 I looked for a while, but was unabl ...
  • 基于文档,YARN旨在允许单个应用程序(通过ApplicationMaster)以共享,安全和多租户方式利用集群资源。 此外,它仍然了解群集拓扑,以便有效地调度和优化数据访问,即尽可能减少应用程序的数据运动。 为了实现这些目标,中央调度程序(在ResourceManager中)具有有关应用程序资源需求的大量信息,这使其能够跨集群中的所有应用程序做出更好的调度决策。 这导致我们到ResourceRequest和由此产生的容器。 本质上,应用程序可以通过ApplicationMaster请求特定的资源请求,以满 ...
  • 在这种情况下,我认为面试官真的想知道为了找到第二大元素,是否必须对整个数组进行排序 (并选择第二个元素)还是有更好的方法 ? 答案是你不必对整个数组进行排序以找到前k个元素。 排序将花费O(nlgn)时间,而找到前k个项目将只需要O(nlogk)。 您可以使用简单的示例来解释答案。 如果你必须在100张卡中找到第二大卡,其数字从低到高。 卡片未分类。 要找到第二大牌,你所要做的就是拿到目前为止你手中看到的前两张牌。 当您选择新卡时,看它是否大于您手中的卡,如果是这样,请用手中最小的卡替换新的卡。 在此过程结 ...
  • 以防万一其他人发现这个错误:看起来这是因为hadoop耗尽了磁盘空间......对于那些简单的事情来说,这是一个非常神秘的错误。 我认为~90GB就足以使用我的30GB数据集了,我错了。 Just in case somebody else stubles upon this error: It seemed like this was caused due to hadoop running out of disc space... Pretty cryptic error for something a ...
  • 我不确定我完全理解Hangfire的范围,但这听起来像我使用Quartz Scheduler解决的同类任务 I'm not sure I understand completely the scope of Hangfire, but it sounds like the same kind of tasks I have solved using Quartz Scheduler
  • 如果您正在使用OOM,则可以调整操作系统以及JVM。 请参考调整性能文档[1] 。 If you are getting OOM, you can tune the Operating System as well as the JVM. Please refer tuning performance documentation [1].
  • 通常在“应用”按钮中 job-application.php (job_manager/job-application.php) 脚本,至少默认情况下。 试着检查那里。 "Apply" button usually in job-application.php (job_manager/job-application.php) script, at least, by default. Try to check there.
  • 我有两个建议,即使可能无法解决您的问题,也可能间接帮助。 不要将pm声明为类级数据成员。 你有这个需要吗? 我从来没有做。 如果可以,请根据需要在方法内声明pm 。 一旦你关闭它,它就可以被丢弃(并且可以最小化你获得异常的机会)。 简化你的pm.close()代码。 如果您不使用交易,为什么不尝试这样的事情: PersistenceManager pm = PMF.get().getPersistenceManager(); try { // // Do your query stuff... ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)