uc浏览器发起ajax请求 报错"readyState":0,"status":0,"statusText":"TypeError: Failed to construct 'URL': Invalid URL"

业务:投放广告的落地页使用的jquery ,发起ajax请求报错 {"readyState":0,"status":0,"statusText":"TypeError: Failed to construct 'URL': Invalid URL"}

原因:uc浏览器里面请求地址写的相对路径,页面发起请求不会带上域名host 其他浏览器则会自动拼接上 

解决:判断是uc浏览器,就拼上域名  

代码

$.ajax({
      type: "GET",
      //url: '/api/send-msg',
      url: navigator.userAgent.indexOf('UCBrowser') > -1? 'https://'+location.host + '/api/send-msg' : '/api/send-msg', // 这里是解决在uc里面发不起请求
      data: {
        mobile: phone,
        courseId: GetUrlParam('course_id') || 2,
      },
      success: function (data) {
        if (data.code !== 0) {
          myAlert('系统提示', data.msg); 
          return;
        } else {
          myAlert('系统提示', '验证码已发送'); 
          isClickGetCode = false;
          countintervalt = setInterval(timecount1, 1000);
          button2.attr("disabled", true);
          setTimeout(function () {
            button2.attr("class", "");
          }, 1000);
        }
      },
      error: function (data) {
        // 成功返回的结果(响应)
        console.log('codeerrordata', JSON.stringify(data));
      },
    });

  

posted @ 2022-10-11 18:22  胖娘子_bug  阅读(1012)  评论(0)    收藏  举报