使用content-type:application / json执行js http post请求

时间:2015-06-22 11:46:42

标签: json node.js sails.js

我想了解如何在http请求如下所示编写sails-js控制器,特别是在这里我尝试使用def do_threading(q,function,argument, timeout_in=1): # Start function as a process p = Process(target=function, args=(argument,q,)) p.start() p.join(.1) if p.is_alive(): # Wait for 'timeout_in' seconds or until process finishes p.join(timeout_in) # If thread is still active if p.is_alive(): print("running... let's kill it...") # Terminate p.terminate() p.join() return False return True def do_big_job(rule, q): # Do something with passed argument print("Child: ", rule) # heavy computation using Pyeda library f = expr2bdd(expr(rule)) count = f.satisfy_count() solution=[] for i in f.satisfy_all(): solution.append(i) # Putting result in the queue for exchange q.put([solution,count]) def main() manager = multiprocessing.Manager() q = manager.Queue() # Initializing Queue for data exchange between processes solved_parts={} timed_out_parts={} for rule in rules: # Iterating over rules and creating process for each rule each_rule={} #Creating new processes to carry out heavy computation and passing the Queue 'q' for data exchange processed = do_threading( q, do_big_job, rule, timeout_in=1) if processed: r = q.get() # Getting result from the queue each_rule["solution"] = r[0] each_rule["solution_count"] = r[1] each_rule["count_unique_var"]=count_unique_variables(rule) else: each_rule["solution"] = "None" each_rule["solution_count"] = "None" each_rule["count_unique_var"]=count_unique_variables(rule) # Putting results in 2 types of lists if each_rule["solution"]=="None": timed_out_parts[part_num]=each_rule.copy() else: solved_parts[part_num]=each_rule.copy() main() 发送原始数据。请指教。 (我可以使用带有表单数据的POST,在控制器中如果读取req.body,我将表单数据作为json的键值对)

Content-Type: application/json

2 个答案:

答案 0 :(得分:2)

尝试用以下方法替换数据:

data: '{ "companyID":"TGDEV","firstName":"Naga","lastName":"A", "otherIDs": [123,345] }'

我认为这与JSON无效有关。单引号不是有效的JSON。如果您不确定,请查看您的JSON,这是一个很好的资源:http://jsonlint.com/

答案 1 :(得分:-1)

用以下代码替换数据:data: '{ "companyID":"TGDEV","firstName":"Naga","lastName":"A", "otherIDs": [123,345] } '