简体   繁体   English

我想将JSON数据传递给与Web视图相关的javascript函数。 但是我无法从Javascript函数中获取响应数据

[英]I want to pass JSON data to javascript function related to web view. But I am unable to get the response data from the Javascript function

When printing 'b' value, I am getting empty. 打印'b'值时,我变空了。 Means the data I have sent was wrong. 意味着我发送的数据是错误的。

Am I doing anything wrong? 我做错了吗?

Here is my code : 这是我的代码

Swift 4.0 Swift 4.0

let inputPayload: Dictionary = ["wellname": ["answer": "firstName"]]

if JSONSerialization.isValidJSONObject(inputPayload) {
  do {
    let rawData =
      try JSONSerialization.data(withJSONObject: inputPayload, options: [])

    let b = webView.stringByEvaluatingJavaScript(from: "downloadComplete(\(rawData))") !
      print(b)
  } catch {

  }
}

This is my Javascript function 这是我的Javascript函数

function downloadComplete(a) {
  return a.wellname.answer
}

Objective-c Objective-C的

- (void)runJavaScriptCallback:(NSString *) jsonString {
    NSString *exec_template = @"downloadComplete(\'%@\');";
    NSString *exec = [NSString stringWithFormat:exec_template, jsonString];

    [webView evaluateJavaScript:exec completionHandler:nil];
}

Swift 4 斯威夫特4

func runJavaScriptCallback(jsonString: String) {
    webView.evaluateJavaScript("downloadComplete(\(jsonString))") { result, error in
        guard error == nil else {
            print(error)
            return
        }
    }
}

Javascript 使用Javascript

var downloadComplete = (bridgeMsg) => {
  // escape newline character from iOS sdp
  bridgeMsg = bridgeMsg.replace(/\r?\n|\r/g, '\\r\\n');

  const msg = JSON.parse(bridgeMsg);
  // do whatever you want with the parsed json object
};

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

相关问题 我想使用网络摄像头拍照,然后将数据传递给JavaScript函数 - I want to take a photo using the webcam, then pass that data to a JavaScript function 如何在javascript中使用函数参数成功传递响应数据? - How can I pass the response data successfully with function parameter in javascript? 将JavaScript数据从视图传递到coffeescript函数 - Pass javascript data from view into coffeescript function Azure Function + Javascript 如何获取我在 post 请求中传递的数据? - Azure Function + Javascript how to get the data I pass in the post request? 为什么我无法在JavaScript中调用函数? - Why am I unable to call a function in JavaScript? 无法使用javascript将值从函数传递到数据层数组 - Unable to pass a value from a function to a data layer array using javascript 无法将数据从 asp.net 动态传递到 javascript function? - Unable to pass data from asp.net to javascript function dynamically? 无法将数据从JavaScript传递到JSON - Unable to pass data from javascript to json Django:将数据JSON从视图传递到javascript - Django: to pass data JSON from view to javascript 我是Java语言的新手,正在从url获取JSON数据,我只能在成功函数中访问数据,我丢失了什么吗? - I'm new to javascript and I'm fetching JSON data from url, I'm only able to access data in success function, Am I missing something?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM