angular - 如何在 Angular 4的http请求中传递url参数

标签 angular httprequest

如何通过传递参数进行get请求?我尝试了这个,但收到 404 错误

getOrderbyOrderNo() {

        this.orderno = this.cookie_service.get('key');

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('orderno', this.orderno );
        let params = new URLSearchParams();
        params.append("someParamKey", this.orderno )

        return this.http.get('http://localhost:8080/View/orders', { headers: headers, params: params })


    }

输出

ERROR 
Object { _body: "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /View/orders</pre>\n</body>\n</html>\n", status: 404, ok: false, statusText: "Not Found", headers: {…}, type: 2, url: "http://localhost:8080/View/orders" }

最佳答案

IMO 有两种方法可以通过 http 请求发送参数。

  1. 将其附加到 URL,如下所示 -

    return this.http.get('http://localhost:8080/View/orders/'+this.orderno, { headers: headers  })
    
  2. 另一种方法是,如果您使用 httpclient 那么您可以在选项中发送参数,如下所示 -

     let httpParams = new HttpParams()
        .set('orderno', this.orderno);
    

有关此内容的更多信息,请阅读此处

get 类型的请求中,没有请求的正文部分,因此您必须在 URL 中附加参数,即参数或查询参数。或使用选项中的第二种方式。

关于angular - 如何在 Angular 4的http请求中传递url参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50247130/

相关文章:

proxy - Kibana 4 代理仪表板嵌入

javascript - 如何在 Ionic 3 和 Angular 4 中禁用表单验证?

angular - 在 Angular 中使用带有 RxJS 的声明式/响应式(Reactive)数据访问方法时如何获取 "pass"数据?

c# - 无法在Unity中使用UnityWebRequest从HTTP响应获取数据

security - 只允许从特定的 html 页面访问图像

node.js - 无法让nodeJS Express post显示正文数据

Angular 7 AmCharts PDF 字体

css - Angular 2 css 样式从组件中泄漏出来

angular - 将数据导出到 excel 时,日期会根据时区发生变化。如何避免?

ruby-on-rails-3 - 如何在 Heroku 中处理长网络请求?