微信支付参数使用ajax返回时,一直出现appid为空的报错

微信支付所需参数,使用ajax的方式返回到页面时,‘getBrandWCPayRequest’,的下一行,直接写data(data是ajax返回的所需参数)一直报:appid不能为空。

原因是ajax返回的是字符串(且与页面赋值这种方法不同)。

处理方式:把data转化成json对象,var data1 = JSON.parse(data),然后在’WeixinJSBridge.invoke’方法中,直接把这个data1写出来,
例:

function jsApiCall()
{
WeixinJSBridge.invoke(
‘getBrandWCPayRequest’,
{
“appId”: data1.appId,
“nonceStr”: data1.nonceStr,
“package”: data1.package,
“paySign”: data1.paySign,
“signType”: data1.signType,
“timeStamp”: data1.timeStamp
},
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res.err_code+’=’+res.err_desc+’=’+res.err_msg);
}
);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Java中的相关库来将微信支付返回参数xml转换为String。其中,可以使用Java中自带的JAXB库,或者第三方库,如Jackson、Dom4j等。 以下是使用Dom4j库将微信支付返回参数xml转换为String的示例代码: ```java import java.io.StringWriter; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XmlToString { public static String xmlToString(String xml) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8"))); StringWriter stringWriter = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(stringWriter, format); writer.write(document); writer.close(); return stringWriter.toString(); } } ``` 以上代码中,使用SAXReader类将xml解析为Document对象,然后使用XMLWriter类将Document对象转换为String。输出的String是格式化后的,如果不需要格式化,可以使用OutputFormat.createCompactFormat()。 这是一个使用Dom4j库的简单示例,也可以使用其他库来实现相同的功能。 ### 回答2: 在Java中将微信支付返回参数xml转换为string,您可以使用DOM解析器来实现。下面是一个示例代码: ```java import org.w3c.dom.Document; import org.xml.sax.InputSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.StringReader; public class XmlToString { public static String convertXmlToString(String xml) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xml)); Document doc = builder.parse(inputSource); doc.getDocumentElement().normalize(); return doc.getDocumentElement().getTextContent(); } catch (Exception e) { e.printStackTrace(); return ""; } } public static void main(String[] args) { String xml = "<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>"; String result = convertXmlToString(xml); System.out.println(result); } } ``` 上述代码中,我们首先创建了一个`DocumentBuilderFactory`和`DocumentBuilder`对象。然后,将传入的xml字符串转换为`InputSource`对象,并使用`DocumentBuilder`解析该对象。接下来,调用`normalize()`方法将xml归一化处理,以便获取标签内容。最后,使用`getTextContent()`方法获取标签内容,并将其返回作为字符串。 在`main`方法中,我们提供了一个xml字符串,并调用`convertXmlToString`方法进行转换。最后,将结果打印出来。 请注意,上述代码只是示例,您可以根据实际需要进行修改和适配。 ### 回答3: 在Java中将微信支付返回参数XML转换为String的方式有多种方法,其中一种常用的方法是使用Java的XML解析库进行解析。 首先,我们可以使用Java的内置库`javax.xml.parsers.DocumentBuilderFactory`和`javax.xml.parsers.DocumentBuilder`来解析XML。具体步骤如下: 1. 创建一个`DocumentBuilderFactory`对象,并通过`newInstance()`方法获取实例。 2. 创建一个`DocumentBuilder`对象,并通过`newDocumentBuilder()`方法获取实例。 3. 使用`DocumentBuilder`对象的`parse()`方法将XML字符串解析为一个`Document`对象。 4. 通过`Document`对象的`getElementsByTagName()`方法获取指定标签名的节点列表。 5. 遍历节点列表,使用`getTextContent()`方法获取每个节点的文本内容,并将其拼接为一个完整的字符串。 以下是一个示例代码: ```java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.io.ByteArrayInputStream; public class XmlToString { public static void main(String[] args) { String xml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx1234567890]]></appid></xml>"; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); NodeList nodeList = doc.getElementsByTagName("*"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); sb.append(node.getNodeName()).append(":").append(node.getTextContent()).append("\n"); } String result = sb.toString(); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码将会输出转换后的字符串: ``` xml:SUCCESS return_code:SUCCESS return_msg:OK appid:wx1234567890 ``` 通过以上步骤,我们可以将微信支付返回参数XML成功转换为一个String对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值