Bowen Huang

Results 1 issues of Bowen Huang

使用@uni/request做jsonp请求的时候,JSONP请求返回异常如下: ![image](https://user-images.githubusercontent.com/35956145/176112676-3b7a385d-9fbe-4a92-8ea0-1c8d7b300905.png) 找到抛出异常的代码如下: ![image](https://user-images.githubusercontent.com/35956145/176112729-25f75add-22b0-49c2-98b9-630b563656e7.png) 从上面看出,抛出异常的一定是line:130 JSON.parse方法,然后看下异常返回的详细信息如下: ![image](https://user-images.githubusercontent.com/35956145/176112784-943e0597-2734-4514-b322-91b9150278ab.png) 从上图可以会导致代码冲突的,就是返回的“;”没有被替换掉。 那么,line: 129这种解码方式是有问题的,看下源码: ``` code const content = res?.data?.replace(`${jsonpCallback}(`, '').replace(')', ''); ``` 这里只是将)替换掉了,没有考虑到 ); 结尾的情况,并且使用replace的字符串替换是不严谨的,可能导致返回的数据里面本身的相同字符串被替换,应该使用正则匹配替换,代码如下: ``` code const reg = new RegExp(`(^${jsonpCallback}\\(|(\\)|\\);)$)`, 'gm');...