简体   繁体   English

使用jquery ajax vs native xhr

[英]Using jquery ajax vs native xhr

I'm not too familiar with JQuery, and I'm trying to make a post request using jquery ajax. 我对JQuery不太熟悉,我正在尝试使用jquery ajax发布帖子请求。 I was previously using xhr, and I'm not totally sure if I'm reworking it correctly. 我之前使用的是xhr,我不确定我是否正确地重新设计它。 If someone could give me some feedback, that would be greatly appreciated! 如果有人能给我一些反馈,那将非常感谢!

Here is my original code with xhr: 这是我用xhr的原始代码:

j("#saveButton").click(function() {
    var ID = j(".selected-list")[0].getAttribute('id');
var subject = j('input').val();
    var blurb = j("#blurb_stream").val();
var date = j('datepicker').val();
if(blurb.length != '' && ID != undefined && subject.length != ''){
xhr.open("POST", envUrl + "streams/" + ID + "/touches?subject=" + subject + "&body=" + blurb + "&schedule_on=" + date, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            console.log('saved!');
            } else {
            alert('not working');
            }
    }
    xhr.send();
    }
});

Here is my adjusted code using ajax: 这是我使用ajax调整的代码:

j("#saveButton").click(function() {
    var ID = j(".selected-list")[0].getAttribute('id');
var subject = j('input').val();
    var blurb = j("#blurb_stream").val();
var date = j('datepicker').val();
var data = new FormData();
data.append("date", "date");
data.append("ID", "ID");
data.append("subject", "subject");
data.append("blurb", "blurb");    
// check this!
    if(blurb.length != '' && ID != undefined && subject.length != ''){
  j.ajax({
    url: 'envUrl + "streams/" + ID + "/touches?subject=" + subject + "&body=" + blurb + "&schedule_on=" + date',
    type: "POST",
    data: data,
    success: function(){
      alert(1);  
    }
  });
}
}

You have a mistake in your url param, try taking the single quotes out: 你的网址参数有误,请尝试单引号:

j.ajax({
    url: envUrl + "streams/" + ID + "/touches?subject=" + subject + "&body=" + blurb + "&schedule_on=" + date,
    type: "POST",
    data: data,
    success: function(){
      alert(1);  
    }
  });

Also, a little bit of background on using HTTP requests. 此外,还有一些关于使用HTTP请求的背景知识。

In general there are two primary ways of passing data to the server. 通常,有两种主要方法可以将数据传递到服务器。 One method is via URL params, in this case it would be a GET request, and you pass the params on the query string (like you've done in your URL). 一种方法是通过URL params,在这种情况下,它将是一个GET请求,并且您在查询字符串上传递params(就像您在URL中所做的那样)。

The other way is via POST. 另一种方式是通过POST。 With the POST verb, you pass data in the body of the request in a delimited format. 使用POST动词,您可以以分隔格式传递请求正文中的数据。 In this case, you would provide the ajax call with the data: param. 在这种情况下,您将使用data:param提供ajax调用。 Doing both is redundant. 两者兼顾是多余的。

Following best practices, you shouldn't use a GET request to modify data (except in some rare circumstances). 遵循最佳实践,您不应使用GET请求来修改数据(在极少数情况下除外)。 If you're modifying data, you should use a POST, PUT, PATCH or other verb. 如果要修改数据,则应使用POST,PUT,PATCH或其他动词。

Way too complex. 方式太复杂了。 With JQuery AJAX is slim. 随着JQuery AJAX很苗条。 Here I replace all data.append() with an inline object. 在这里,我用内联对象替换所有data.append() I've also removed the query string because it's the same information as in the data block. 我还删除了查询字符串,因为它与数据块中的信息相同。 I've also fixed the URL line, which has some misplaced quotes: 我还修复了URL行,它有一些错误的引号:

j.ajax({
  url: envUrl + "streams/" + ID + "/touches",
  type: "POST",
  data: { "date": date, "ID": ID, "subject": subject, "blurb": blurb },
  success: function () {
    alert(1);  
  }
});

From here you can replace the date/ID/subject/blurb with your fetch methods. 在这里,您可以使用fetch方法替换date/ID/subject/blurb Example: 例:

j.ajax({
  url: envUrl + "streams/" + ID + "/touches",
  type: "POST",
  data: {
    "date":    j('datepicker').val(),
    "ID":      j(".selected-list")[0].getAttribute('id'),
    "subject": j('input').val(),
    "blurb":   j("#blurb_stream").val()
  },
  success: function () {
    alert(1);  
  }
});

The whole script is now: 整个脚本现在是:

j("#saveButton").click(function () {
  j.ajax({
    url: envUrl + "streams/" + ID + "/touches",
    type: "POST",
    data: {
      "date":    j('datepicker').val(),
      "ID":      j(".selected-list")[0].getAttribute('id'),
      "subject": j('input').val(),
      "blurb":   j("#blurb_stream").val()
    },
    success: function () {
      alert(1);  
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM