js发送get请求在url后面添加参数为json对象报错解决方案?

js发送get请求在url后面添加参数为json对象报错解决方案?

(1)var obj1=JSON.stringify(obj);将对象转化成JSON字符串

(2)'${ctx}/gis/view?obj='+encodeURIComponent(obj1, 'utf-8');在请求中加上encodeURIComponent进行编码处理

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码使用 `httpclient` 发送 GET 请求参数JSON: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONObject; public class HttpClientGetWithJson { public static void main(String[] args) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(); // 设置请求头 getRequest.addHeader("accept", "application/json"); getRequest.addHeader("Content-Type", "application/json"); // 构造请求参数JSON格式 JSONObject json = new JSONObject(); json.put("key1", "value1"); json.put("key2", "value2"); StringEntity entity = new StringEntity(json.toString()); // 设置请求参数 getRequest.setURI(new URI("http://example.com/api/endpoint?" + entity)); getRequest.setEntity(entity); // 发送请求并获取响应 HttpResponse response = httpClient.execute(getRequest); BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; while ((output = br.readLine()) != null) { System.out.println(output); } httpClient.getConnectionManager().shutdown(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建了一个 `DefaultHttpClient` 实例,然后创建一个 GET 请求并设置请求头。接下来,我们将请求参数构造为 JSON 格式,并将其添加请求中。最后,我们发送请求并获取响应,将响应输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值