最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Send JSON data to Server via JavaScript - Stack Overflow

programmeradmin4浏览0评论

I've a service to consume, the details of the service are as follows.

POST /API/v02/JsonWs.svc/json/V02 HTTP/1.1 [OR] POST /API/Public/v02/JsonWs.svc/json/V02 HTTP/1.1
 Host: dev.domain.us
 Content-Type: application/json; charset=utf-8
Cookie: LogicAuth=3821F201156EA833......; path=/; HttpOnly

{"Header":{"Procedure":"Family_Insert"},"Body":{"EmailAddress":"[email protected]","Password":"test14","IdPhoneNumber":3051234567,"Address":{"Line1":"1234 Avenue","Line2":"Suite 900","City":"Coral","ZipCode":"33327","State":{"IdState":10}},"TimeZone":{"IdTimeZone":-5},"User":{"IdGender":1,"NameFirst":"Tom","NameMiddle":"Larry","NameLast":"Lewis","DateBirth":"10-21-1978","ClubName":"ABC","Communities":[1,2],"UserConnectTypes":[3,4]}}}

The javascript code to initiate from:

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("POST", "/json-handler");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

In your answer, you don't have to use all variables, I just need some help to get me going...

Edit: Please specify the next steps of how to send json data to the server after I set the request headers in xmlhttp. I'm not sure what URL to send data to the above service.

On the side can someone tell me how to test the above service in chrome postman?

I've a service to consume, the details of the service are as follows.

POST /API/v02/JsonWs.svc/json/V02 HTTP/1.1 [OR] POST /API/Public/v02/JsonWs.svc/json/V02 HTTP/1.1
 Host: dev.domain.us
 Content-Type: application/json; charset=utf-8
Cookie: LogicAuth=3821F201156EA833......; path=/; HttpOnly

{"Header":{"Procedure":"Family_Insert"},"Body":{"EmailAddress":"[email protected]","Password":"test14","IdPhoneNumber":3051234567,"Address":{"Line1":"1234 Avenue","Line2":"Suite 900","City":"Coral","ZipCode":"33327","State":{"IdState":10}},"TimeZone":{"IdTimeZone":-5},"User":{"IdGender":1,"NameFirst":"Tom","NameMiddle":"Larry","NameLast":"Lewis","DateBirth":"10-21-1978","ClubName":"ABC","Communities":[1,2],"UserConnectTypes":[3,4]}}}

The javascript code to initiate from:

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("POST", "/json-handler");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

In your answer, you don't have to use all variables, I just need some help to get me going...

Edit: Please specify the next steps of how to send json data to the server after I set the request headers in xmlhttp. I'm not sure what URL to send data to the above service.

On the side can someone tell me how to test the above service in chrome postman?

Share Improve this question edited Aug 29, 2017 at 19:16 Maslow 18.8k22 gold badges113 silver badges194 bronze badges asked Sep 25, 2014 at 14:52 user2727195user2727195 7,34020 gold badges73 silver badges121 bronze badges 2
  • What is your question exactly? I don't see a question. – vrijdenker Commented Sep 25, 2014 at 14:55
  • edited to made it clear, what's the next step, how to encode json data for the server, – user2727195 Commented Sep 25, 2014 at 14:57
Add a ment  | 

1 Answer 1

Reset to default 4
xmlhttp.send('{"Header":{"Procedure".......');

Basically, you need to construct a string of your JSON, and call xmlhttp.send(jsonStr).

One way to create that JSON string is like this:

var jsonStr = JSON.stringify({
  Header: {
    Procedure: 'Family_Insert'
  },
  Body: ....
});

Also, you seem to be using the wrong address. Complete sample:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://dev.domain.us/API/v02/JsonWs.svc/json/V02');
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
var jsonStr = JSON.stringify({
   Header: {
      Procedure: 'Family_Insert'
   },
   Body: ....
});
xhr.send(jsonStr);
发布评论

评论列表(0)

  1. 暂无评论