JavaScript-WeChat Enterprise number: How to post JSON data to send messages to enterprise number members

Source: Internet
Author: User
Tags hasownproperty

To send a message to an enterprise number member, you must post the JSON data to the specified URL that contains access_token according to the message interface message data format of the Enterprise Developer documentation.

What I want to achieve is that every interval, after querying the database, sends a message to a specific member based on the result of the query.

I have been able to post JSON data successfully by writing commands on my own Linux server Shell curl , and I have received a message on my own phone that shows my understanding of the document and the format of the data is not a problem. However, this approach must take the acquired Access_token hard code to the command line, and TOKEN is time-limited, so it can only be used for testing, not in the production environment.

In a production environment, I think the solution is to write a php/javascript program to send messages, and then execute the PHP program on the server side with a cron job. However, due to the lack of understanding of the post principle, the concrete implementation process encountered many problems.

First of all, if only consider the feasibility, can you use jquery Ajax method to achieve it?
In my experiment, I first used PHP to get the correct token, built the URL, and then passed the URL value to the JavaScript variable URL, in the console to try

$.ajax({  type: "POST",  url: url,  data: '{"touser":"Jacklyn","msgtype":"text","agentid":23,"text":{"content":"test message"}}',  success: function(){},  dataType: "json",  contentType : "application/json"});

Console returned a cross domain error, and I understand that because of cross domain, I can't see the return of a successful or failed message.

But I did not receive any messages on my phone, so the delivery should be a failure. In order to see what data the above code sends, I use another file to receive the data:


  
   ';  var_dump($_POST);  echo "
"; ?>

But the object returned in $.ajax. Responstext can see the result of the post is

Array (0) {}

If I get rid of Datetype and ContentType in Ajax methods, I can see the correct data in ResponseText. So, the first question is, if the JSON-formatted data sent by $.ajax cannot be received by $_post, how should the data be read on the server side?

In addition, I read some of the post documents, if not understood correctly, the actual post transmission of information consists of two parts, one is the header, one is data; I also searched for some articles about how to post JSON data through PHP, not very well read, It seems that most of the articles are about a certain degree of control of the header before the post JSON, does that mean that JavaScript is not possible?

Finally, I use the following function

function Gotourl (path, params, method) {//null Check method = Method | | "POST";  Set method to post by default if not specified.  The rest of this code assumes is not using a library.  It can be made less wordy if you use one.  var form = document.createelement ("form");  Form.setattribute ("Method", method);  Form.setattribute ("Action", Path);      Fill The Hidden form if (typeof params = = = ' string ') {var HiddenField = document.createelement ("input");      Hiddenfield.setattribute ("type", "hidden");      Hiddenfield.setattribute ("name", ' data ');      Hiddenfield.setattribute ("value", params);  Form.appendchild (HiddenField); } else {for (var key in params) {if (Params.hasownproperty (key)) {var HiddenField = Documen              T.createelement ("input");              Hiddenfield.setattribute ("type", "hidden");              Hiddenfield.setattribute ("name", key); if (typeof params[key] = = = ' object ') {hiddenfield.setattrIbute ("Value", Json.stringify (Params[key]));              } else{Hiddenfield.setattribute ("value", Params[key]);          } form.appendchild (HiddenField);  }}} document.body.appendChild (form); Form.submit ();}

Simulate a form submission data, and then I try to

gotoUrl(url,'{"touser":"shenkwen","msgtype":"text","agentid":23,"text":{"content":"test message"}}')给url加上一个debug=1参数的话,可以看到postdata,以上代码的postdata是这样的:

data=%7b%22touser%22%3a%22shenkwen%22%2c%22msgtype%22%3a%22text%22%2c%22agentid%22%3a23%2c%22text%22%3a%7b% 22content%22%3a%22test+message%22%7d%7d

It seems that the JSON string is UrlEncode, does this mean that the JSON data requested in this case cannot be submitted on a form-by-application basis?

Reply content:

To send a message to an enterprise number member, you must post the JSON data to the specified URL that contains access_token according to the message interface message data format of the Enterprise Developer documentation.

What I want to achieve is that every interval, after querying the database, sends a message to a specific member based on the result of the query.

I have been able to post JSON data successfully by writing commands on my own Linux server Shell curl , and I have received a message on my own phone that shows my understanding of the document and the format of the data is not a problem. However, this approach must take the acquired Access_token hard code to the command line, and TOKEN is time-limited, so it can only be used for testing, not in the production environment.

In a production environment, I think the solution is to write a php/javascript program to send messages, and then execute the PHP program on the server side with a cron job. However, due to the lack of understanding of the post principle, the concrete implementation process encountered many problems.

First of all, if only consider the feasibility, can you use jquery Ajax method to achieve it?
In my experiment, I first used PHP to get the correct token, built the URL, and then passed the URL value to the JavaScript variable URL, in the console to try

$.ajax({  type: "POST",  url: url,  data: '{"touser":"Jacklyn","msgtype":"text","agentid":23,"text":{"content":"test message"}}',  success: function(){},  dataType: "json",  contentType : "application/json"});

Console returned a cross domain error, and I understand that because of cross domain, I can't see the return of a successful or failed message.

But I did not receive any messages on my phone, so the delivery should be a failure. In order to see what data the above code sends, I use another file to receive the data:


  
   ';  var_dump($_POST);  echo "
"; ?>

But the object returned in $.ajax. Responstext can see the result of the post is

Array (0) {}

If I get rid of Datetype and ContentType in Ajax methods, I can see the correct data in ResponseText. So, the first question is, if the JSON-formatted data sent by $.ajax cannot be received by $_post, how should the data be read on the server side?

In addition, I read some of the post documents, if not understood correctly, the actual post transmission of information consists of two parts, one is the header, one is data; I also searched for some articles about how to post JSON data through PHP, not very well read, It seems that most of the articles are about a certain degree of control of the header before the post JSON, does that mean that JavaScript is not possible?

Finally, I use the following function

function Gotourl (path, params, method) {//null Check method = Method | | "POST";  Set method to post by default if not specified.  The rest of this code assumes is not using a library.  It can be made less wordy if you use one.  var form = document.createelement ("form");  Form.setattribute ("Method", method);  Form.setattribute ("Action", Path);      Fill The Hidden form if (typeof params = = = ' string ') {var HiddenField = document.createelement ("input");      Hiddenfield.setattribute ("type", "hidden");      Hiddenfield.setattribute ("name", ' data ');      Hiddenfield.setattribute ("value", params);  Form.appendchild (HiddenField); } else {for (var key in params) {if (Params.hasownproperty (key)) {var HiddenField = Documen              T.createelement ("input");              Hiddenfield.setattribute ("type", "hidden");              Hiddenfield.setattribute ("name", key); if (typeof params[key] = = = ' object ') {hiddenfield.setattrIbute ("Value", Json.stringify (Params[key]));              } else{Hiddenfield.setattribute ("value", Params[key]);          } form.appendchild (HiddenField);  }}} document.body.appendChild (form); Form.submit ();}

Simulate a form submission data, and then I try to

gotoUrl(url,'{"touser":"shenkwen","msgtype":"text","agentid":23,"text":{"content":"test message"}}')给url加上一个debug=1参数的话,可以看到postdata,以上代码的postdata是这样的:

data=%7b%22touser%22%3a%22shenkwen%22%2c%22msgtype%22%3a%22text%22%2c%22agentid%22%3a23%2c%22text%22%3a%7b% 22content%22%3a%22test+message%22%7d%7d

It seems that the JSON string is UrlEncode, does this mean that the JSON data requested in this case cannot be submitted on a form-by-application basis?

ContentType can also be specified as UrlEncode.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.