DELETE请求在Insomnia/Postman正常,前端调用报错求助
问题描述
DELETE请求在Insomnia/Postman中可正常返回204响应码,但前端发起该请求时出现报错。以下是相关代码及信息:
服务端Service代码(C#)
public bool Dissociate(string envCode, string serialNumber, string gatewayType) { envCode.ThrowIfNull(); serialNumber.ThrowIfNull(); gatewayType.ThrowIfNull(); if (!IsAssociated(envCode, serialNumber, gatewayType)) { _logService.Warn($"DspService - Dissociate - {gatewayType} {serialNumber} not associated to DSP tenant on environment {envCode}"); return false; } EnvironmentSettings dspEnvSettings = GetEnvironmentSettings(envCode); string baseUri = DspHelper.GetBaseUriByAction(dspEnvSettings, DspActionType.Dissociate); string dspDeviceId = DspHelper.BuildDeviceId(serialNumber, gatewayType, true); string uri = $"{baseUri}/registry/devices/{dspDeviceId}/claim"; var body = new { Claimable = true }; try { var authToken = GetAuthorizationHeader(dspEnvSettings, DspActionType.Dissociate); RunDspCommand<bool>(dspEnvSettings, uri, authToken, body, HttpMethod.Put, DspActionType.Dissociate); return true; } catch (Exception e) { _logService.ErrorException(e, $"DspService - Dissociate - Error dissociating {gatewayType} {serialNumber} on environment {envCode}. {uri}"); throw; } }
Insomnia响应说明
- 请求返回状态码:204 No Content
- 请求参数及响应详情符合预期,请求成功完成
服务端Controller代码(C#)
[AcceptVerbs("DELETE")] [Route("dissociate")] public bool Dissociate([FromUri] string gatewayType, [FromUri] string gatewaySerial) { if (string.IsNullOrEmpty(gatewaySerial) || string.IsNullOrEmpty(gatewayType)) { this.BadRequestResponse(); } var connectedUser = this.permissionService.GetConnectedUser(); this.permissionService.CheckRolePermission(connectedUser.Role, PermissionConstant.DissociateComX); bool hasSucceeded = this.dspService.Dissociate(connectedUser.CurrentEnvironment, gatewaySerial, gatewayType); if (hasSucceeded) { this.applicationDataAccess.LogAction(connectedUser.CurrentEnvironment, connectedUser.Email, LogActionConstants.Action.DissociateComX, string.Format(LogActionConstants.Message.DissociateComX, gatewayType, gatewaySerial)); } else { this.BadRequestResponse("cannot deprovioned comx"); } return hasSucceeded; }
异常位置
异常出现在Service代码的以下行:
RunDspCommand<bool>(dspEnvSettings, uri, authToken, body, HttpMethod.Put, DspActionType.Dissociate);
前端代码
controller.ts
dissociateGateway() { if (!this.isDspGateway || this.isLoadingDspState || this.isDissociating || this.gateway.FirmwareUpgradeWorkflow || this.gateway.DeleteGatewayWorkflow || !this.isAssociated()) { return; } this.modalService.confirm( `The ${this.gateway.Type} ${this.gateway.SerialNumber} will be dissociated from its DSP tenant ${this .dspGatewayState.Tenant}.`, 'Are you sure you want to dissociate the gateway from DSP tenant?') .then(() => { this.isDissociating = true; this.dspService.dissociate(this.gateway.Type, this.getDeviceId()) .then(() => this.getDspGatewayState()) .catch((e) => { if (e) { this.modalService.error('Error dissociating the gateway. Please retry later'); } }) .finally(() => this.isDissociating = false); }); }
service.ts
dissociate(gatewayType: string, gatewaySerial: string): ng.IPromise<boolean> { var uri = this.ROUTES.dissociate .replace('{:gatewayType}', gatewayType.toString()) .replace('{:gatewaySerial}', gatewaySerial); return this.restService .delete(uri, null) .then((response: any) => { return response; }) .catch((response: any) => { return this.$q.reject(response); }); }
路径配置
dissociate: '/dsp/dissociate?gatewaytype={:gatewayType}&gatewaySerial={:gatewaySerial}',
需要更多信息请告知,恳请协助解决该问题。
内容的提问来源于stack exchange,提问作者Philip
相关产品推荐
相关产品推荐

