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

javascript - How to jQuery.post() with no data to send - Stack Overflow

programmeradmin1浏览0评论

I'm not sure if I'm approaching this the correct way but I'm trying to send a small amount of data to the server and receive a couple of strings back. Because of the way the server CMS works the data is most easily sent in the URL path so I have no need to send any additional 'data'. For example :

      var url = '/footnotes/cleartile/'+nid+'/'+tid+'/'+side;
      var mydata = 'This serves no purpose';
      jQuery.post(url, mydata, function(data) {
        console.log("Data Loaded: " + data);
      });

Is jQuery.post() the correct mechanism for this type of munication ? And if so, what should I pass in the data parameter when nothing is necessary ?

I'm not sure if I'm approaching this the correct way but I'm trying to send a small amount of data to the server and receive a couple of strings back. Because of the way the server CMS works the data is most easily sent in the URL path so I have no need to send any additional 'data'. For example :

      var url = '/footnotes/cleartile/'+nid+'/'+tid+'/'+side;
      var mydata = 'This serves no purpose';
      jQuery.post(url, mydata, function(data) {
        console.log("Data Loaded: " + data);
      });

Is jQuery.post() the correct mechanism for this type of munication ? And if so, what should I pass in the data parameter when nothing is necessary ?

Share Improve this question asked Jun 28, 2014 at 14:10 elbelb 1771 silver badge8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

To keep the same function I would use this:

var url = '/footnotes/cleartile/'+nid+'/'+tid+'/'+side;
jQuery.post(url, {} , function(data) {
   // The data here represents the answer from the server
   console.log("Data Loaded: " + data);
});

or

jQuery.post(url, function(data) {
   // The data here represents the answer from the server
   console.log("Data Loaded: " + data);
});

$.post doesn't require the data argument at all, it's listed as optional. You can just leave it out:

var url = '/footnotes/cleartile/'+nid+'/'+tid+'/'+side;
jQuery.post(url, function(data) {
    console.log("Data Loaded: " + data);
});

You probably want to use jQuery.get in this case (if the server does accept a GET rather than only a POST, which is most certainly the case here).

发布评论

评论列表(0)

  1. 暂无评论