mysql - 无法在本地开发服务器上通过 Angular 2 中的 HTTP POST 发布数据

标签 mysql django angular

我是网络开发的新手,正在尝试让我的第一个应用程序使用 Angular 2 工作。我有一个 react 形式,我正在从中构建一个包含抬头和项目信息的示例发票。当我尝试将数据发布到同一网络中另一台机器上托管的数据库中时,我在 Observable 运算符 map 上收到一些错误,指出 "[Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. at Function.remoteFunction (<anonymous>:2:14)]/

代码如下: 为简单起见,我创建了一个变量 sampleInvoice,它具有服务器可接受的预格式化 JSON 结构。 我已经尝试使用 Chrome 的 REST 扩展来发布这些数据并且它工作正常。

发票.service.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of }         from 'rxjs/observable/of';
import 'rxjs/add/operator/delay';
import { Http, Headers, RequestOptions,Response, RequestMethod} from '@angular/http';
import { Invoice, invoices } from './invoice';

    createInvoice(invoice: Invoice): Observable <Invoice>{
    let sampleInvoice = {
    "invoiceDate":"2017-03-21",
    "mobileNumber": 8297088993,
    "salesPerson": "Ramesh",
                "paymentMode": "CASH",
                "numOfItems": 1,
                "grossAmount": "2100.00",
                "totalDiscount": "200.00",
                "totalTax":"100.00",
                "netAmount": "2000.00",    
                "paidAmount": "2000.00",
                "items": [
                            {
                                "lineItem": 1,
                                "productBarcode": "product1",
                                "quantity": 1,
                                "itemGrossAmount":"1200.00",
                                "itemDiscount": "50.00",
                                "itemTax":"50.00",
                                "itemNetAmount":"1200.00"
                            },
                            {
                                "lineItem": 2,
                                "productBarcode": "product2",
                                "quantity": 2,
                                "itemGrossAmount":"400.00",
                                "itemDiscount": "50.00",
                                "itemTax":"50.00",
                                "itemNetAmount":"800.00"
                            }
        ]
    }
    let headers = new Headers({"Content-Type": "application/json"});

    console.log(headers);
    console.log(JSON.stringify(sampleInvoice));

    return this.http.post("http://192.168.0.9:8000/api/invoice/create/",JSON.stringify(sampleInvoice),{headers})
                              .map(res => res.json());
}

注意:服务器(django 和 mySQL)托管在另一台计算机上提到的 URL。 非常感谢任何解决此问题的帮助。

问候, 纳文

最佳答案

问题出在我编写 Observable 的方式上。我错过的是我没有订阅响应,而是在响应中使用了 catch 运算符。由于没有订阅,Observable 被标记为 Cold 并且没有发出任何网络请求。这是更新后的 createInvoice 代码片段

return this.http.post(this.invoiceUrl+"create/",bodyString,options)
                          .map((res:Response) => res.json())
                          .subscribe((res)=>{
                              if (res){
                                  console.log(res);
                                  return res
                                }});

关于mysql - 无法在本地开发服务器上通过 Angular 2 中的 HTTP POST 发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42971388/

相关文章:

Angular - 如何修复 'property does not exist on type' 错误?

java - 如何解决hibernate建表时提示创建表不成功的问题

MySQL 自动 ID 列

python - LiveServerTestCase 服务器看到不同的数据库进行测试

Django 管理员 : list_display() order by count of related object

javascript - 是否有可能以某种方式在 Angular 路由守卫上使用否定运算符?

php - mysql 多表外键? - 初学者

php - MySQL 真正的转义字符串

python - django - 下载并安装 django rest 框架

angular - Angular 中的 Twitter API Request_Token