简体   繁体   English

Ajax从jquery到javascript

[英]Ajax from jquery to javascript

I want to convert an ajax function from jquery to plain javascript 我想将ajax函数从jquery转换为纯javascript

I have tried this but it doesn't react the same way as the url doesn't receieve the response when i try with my plain js 我已经尝试过了,但是当我尝试使用普通js时,它不会以与URL无法接收响应的方式反应不同

Here is my jquery 这是我的jQuery

(function ($){
try{
        var event_category = 'a';
        var event_name = 'b';
        var page_url = 'c';
        var url = "myurl";
        var data = {
                event_category: event_category,
                event_name: event_name,
                page_url: page_url
        };
        $.ajax({
                crossDomain: true,
                type: "POST",
                url: "myurl",
                data : {event_category: event_category, 
                        event_name: event_name, 
                        page_url: page_url
                        }
        });
  } catch(e){console.log(e)};  
})(jQuery);

And here is what i tried 这是我尝试过的

var event_category = 'action';
var event_name = 'click';
var page_url = 'test';
var request = new XMLHttpRequest();
request.open('POST', 'myurl');
request.setRequestHeader("Content-Type", "application/json; utf-8");
          params = {
              event_category: event_category, 
              event_name: event_name, 
              page_url: page_url
              } 
request.send(JSON.stringify(params));

not sure what i should change 不知道我应该改变什么

Edit: 编辑:

Base on one of the comments i check the network data on the developer tools The jquery is having a response on the header of this format enter image description here enter image description here 根据评论之一,我在开发人员工具上检查网络数据jQuery在此格式的标头上有响应在此处 输入图像描述在 此处 输入图像描述

But the javascript is sending the data is this format 但是javascript发送的数据是这种格式

enter image description here 在此处输入图片说明

Basically the javascript is not sending it on a url params format. 基本上,javascript不会以url params格式发送它。 Not sure how to force it on how to send it on the same format 不确定如何强制以相同格式发送

Is there any reason not to use the fetch API (it can be polyfilled in crappy browsers...)? 有什么理由不使用提取API(可以在糟糕的浏览器中对其进行填充吗?)?

const ajax = async function(url, data) {
  try {

    const response = await fetch(url, {
      credentials: 'include', // like jQuery $.ajax's `crossDomain`
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      // this mimics how jQuery sends POST data as querystring by default
      body: Object.entries(data).map(([key, val]) => `${key}=${val}`).join('&'),
    });

    data = await (
      response.headers.get('content-type').includes('json')
      ? response.json()
      : response.text()
    );

    console.log(data);

    return data;
  } catch(err) { console.log(err) };
}

ajax('myurl', {
  event_category: 'a', 
  event_name: 'b', 
  page_url: 'c',
});

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

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