Home Web Front-end JS Tutorial Front-end WeChat sharing jssdk config:invalid signature signature error solution

Front-end WeChat sharing jssdk config:invalid signature signature error solution

Mar 10, 2018 am 09:44 AM
config invalid javascript

This time I will bring you the solution to the signature error of jssdk config:invalid signature shared on front-end WeChat. The solution to the signature error of jssdk config:invalid signature shared on front-end WeChat What are the precautions? What are the following? Let’s take a look at the case.

About the front-end WeChat sharing jssdk config:invalid signature signature error

I just finished writing on WeChat in the past few days. You may not believe it when I tell you. I wrote a WeChat sharing and used it together 2 weeks. There were all kinds of sadness in the process. At first, the big brother in the backend helped me write the backend visa, but it didn’t work. I wanted to ask him to change it. But later, the company had a project that was eager to go online, and there was no time. The product was urgently needed. I have no choice but to start handling the background by myself.

Okay, without further ado, let’s start with the main text

First of all, it is certain to read the WeChat SDK documentation for WeChat

because I am a front-end person, so I only write code about the front-end. I also cover the back-end. At the end of the article, I will attach the back-end code I modified

$.ajax({
        type: "post",
        dataType: 'json',
        url: '接口链接',
        data: {
            url: urld
        },
        success: function(conf) {
            console.log(conf);
            configWxAPI(conf);
            var conf = conf.url;            return conf;
        },
        error: function(event, XMLHttpRequest, ajaxOptions, thrownError) {            
        }
    });

    //配置权限
    function configWxAPI(conf) {
        wx.config({
            debug: false, //开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端
            //打开,参数信息会通过log打出,仅在pc端时才会打印
            appId: "唯一的ID", //必填,公众号的唯一标识
            timestamp: conf.timestamp, //必填,生成签名的时间戳
            nonceStr: conf.nonceStr, //必填,生成签名的随机串
            signature: conf.signature, //必填,签名
            jsApiList: [                
                'onMenuShareTimeline',                'onMenuShareAppMessage',                'onMenuShareQQ',                'onMenuShareWeibo',                'previewImage',
                
            ] //必填,需要使用的JS接口列表,也就是配置你想使用的调用接口
        });
//      微信分享
        wx.ready(function() {
          //分享的图片
            var imgUrl = ""
                      //分享朋友
            wx.onMenuShareAppMessage({
                title: "标题",
                desc: "内容简介",
                link: "分享链接",
                imgUrl: imgUrl,
                success: function() {
                    //分享成功之后执行的回调函数
                },
                cancel: function() {
                    //取消分享之后执行的回调函数
                }
            });
                             //分享朋友圈
            wx.onMenuShareTimeline({
                title: "标题",
                desc: "内容简介",
                link: "分享链接",
                imgUrl: imgUrl,
                success: function() {
                    //分享成功之后执行的回调函数
                },
                cancel: function() {
                    //取消分享之后执行的回调函数
                }
            });
        });
        wx.error(function(conf) {
            console.log(conf);
            // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
        });
    };
Copy after login

Backend part code

public function getSignPackage() {    
    if(IS_POST){        if(!I('post.url')) $this->ajaxReturn(array('status'=>0,'msg'=>'请输入当前的URL'));
        $jsapiTicket = $this->getJsApiTicket();
          //这句代码微信SDK上面没有介绍,但是用来签证的url里面就&后面会自动带有&,所以这里用了php的字符串替换
        $url =  str_replace("&","&",I('post.url'));

        $timestamp = time();
        $nonceStr = $this->createNonceStr();
    
        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
Copy after login

The time here also has the same random string passed to the front desk, and the url link is passed from the front desk to the backend, and the backend uses this url to perform the following encoding

Remember the url passed to the backend It must be exactly the same as the current link. Remember that the url passed to the backend must be exactly the same as the current link. Remember that the url passed to the backend must be exactly the same as the current link.

  $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
    $signature = sha1($string);
    $signPackage = array(    "jsapiTicket" =>$jsapiTicket,      "appId"     => $this->appId,      "nonceStr"  => $nonceStr,      "timestamp" => $timestamp,      "url"       => $url,      "signature" => $signature,      "rawString" => $string
    );    $this->ajaxReturn($signPackage);
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the php Chinese website!

Related reading:

Front-end entry to css3

axios how to make HTTP request client based on Promise

css gradient color

The above is the detailed content of Front-end WeChat sharing jssdk config:invalid signature signature error solution. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles