从web api 2获取值时出现Json Parse错误(Json Parse Error when getting value from web api 2)
我开发了一个简单的WebAPI 2应用程序,其中包含此get方法:
public HttpResponseMessage Get(int id) { var x = db.TESTS.ToList(); var formatter = new JsonMediaTypeFormatter(); return Request.CreateResponse(HttpStatusCode.OK, x, formatter); }
我检查了。 它返回Json值。 我也在Fiddler检查了它。
但是当我想在一个简单的html文件中检索它时,我得到解析错误。 我的Web服务发布在localhost中。
网站服务网址: http : //192.168.5.154/mobileapi/api/values/2正在返回
[{"ID":2.0,"TEXT":"test 2"},{"ID":1.0,"TEXT":"test"}]
这是代码。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Get Json value from WebAPI url</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <script type="text/javascript"> jQuery(document).ready(function ($) { jQuery.ajax({ type: "GET", url: "http://192.168.5.154/mobileapi/api/values/2?callback=?", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data, status, jqXHR) { alert("data is available"); }, error: function (jqXHR, status) { alert(status); } }); }); </script> </body> </html>
I have developed a simple WebAPI 2 application which include this get method:
public HttpResponseMessage Get(int id) { var x = db.TESTS.ToList(); var formatter = new JsonMediaTypeFormatter(); return Request.CreateResponse(HttpStatusCode.OK, x, formatter); }
I checked. It is returning Json value. I also checked it in Fiddler.
But when I want to retrieve this in a simple html file, I am getting parse error. My Web Service is published in localhost.
Web service url: http://192.168.5.154/mobileapi/api/values/2 is returning
[{"ID":2.0,"TEXT":"test 2"},{"ID":1.0,"TEXT":"test"}]
Here's the code.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Get Json value from WebAPI url</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <script type="text/javascript"> jQuery(document).ready(function ($) { jQuery.ajax({ type: "GET", url: "http://192.168.5.154/mobileapi/api/values/2?callback=?", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data, status, jqXHR) { alert("data is available"); }, error: function (jqXHR, status) { alert(status); } }); }); </script> </body> </html>
原文:https://stackoverflow.com/questions/32069229
最满意答案
琐碎但不便携的方式:
subprocess.call(["/bin/sh","-c","cat File | grepcidr -f file.name >>output.file"])
它需要
/bin/sh
,如果依赖cat
,这应该不是问题。 我认为可以自己构建一个合作子进程系列:你必须做/bin/sh
对管道和重定向的处理,但是在python中。Trivial but non-portable way:
subprocess.call(["/bin/sh","-c","cat File | grepcidr -f file.name >>output.file"])
It requires
/bin/sh
, which should not be a problem if you rely oncat
anyway. I think it's possible to build a family of cooperating subprocesses yourself: you'd have to do what/bin/sh
does with pipes and redirections, but in python.
相关问答
更多-
只需创建一个调用这两个方法的方法。 在为此创建额外功能方面,没有任何耻辱。 这是一个更易于维护的解决方案,试图将一堆代码塞入lambda。 def on_restart(self): self.restartExternal() self.restartWinow.destroy() buttonRestart = Button(..., command = self.on_restart) Just create a method that calls the two methods. ...
-
如果你在文件中有这些行(比如run-python-scripts.sh ),你可以用sh运行它: sh run-python-scripts.sh If you have those lines in a file (say, run-python-scripts.sh), you can run it with sh: sh run-python-scripts.sh
-
您最好将其存储为脚本,而不是试图管理混乱的系统调用: 文件:my.py print "hi" print "hi" 然后从R: > system("python my.py") hi hi 如果您的实际代码复杂得多,您可以生成python代码并将其写入到您随后调用的文件中。 You're better off storing it as a script than trying to manage a messy system call: file: my.py print "hi" print "hi ...
-
在exec_command执行之后,只有paramiko关闭了通道,并且ssh返回一个auth提示符。 似乎它不可能与paramiko ,尝试fabric或其他工具。 ** fabric也没有解决 。 With just paramiko after the exec_command executes the channel is closed and the ssh returns an auth prompt. Seems its not possible with just paramiko, try ...
-
带有|的多个命令(Multiple Commands with | in Python)[2023-04-23]
琐碎但不便携的方式: subprocess.call(["/bin/sh","-c","cat File | grepcidr -f file.name >>output.file"]) 它需要/bin/sh ,如果依赖cat ,这应该不是问题。 我认为可以自己构建一个合作子进程系列:你必须做/bin/sh对管道和重定向的处理,但是在python中。 Trivial but non-portable way: subprocess.call(["/bin/sh","-c","cat File | grep ... -
Python - 一个接一个地执行多个shell命令(Python - Executing multiple shell commands one after another)[2022-05-24]
这段代码有用 from subprocess import Popen, PIPE from time import sleep from fcntl import fcntl, F_GETFL, F_SETFL from os import O_NONBLOCK, read # run the shell as a subprocess: p = Popen(['python', 'shell.py'], stdin = PIPE, stdout = PIPE, stderr = PIP ... -
Python:如何获得多个系统命令的最终输出?(Python: how to get the final output of multiple system commands?)[2021-11-17]
只需将shell=True选项传递给子进程即可 import subprocess subprocess.check_output('ps -ef | grep something | wc -l', shell=True) Just pass the shell=True option to subprocess import subprocess subprocess.check_output('ps -ef | grep something | wc -l', shell=True) -
严格来说,无论您在远程服务器上执行多少命令,您只能使用Popen执行一个命令。 那个命令是ssh 。 要在远程服务器上执行多个命令,只需在command字符串中分隔; S: commands = ["echo 'hi'", "echo 'another command'"] subprocess.Popen([ "ssh", "-o UserKnownHostsFile=/dev/null", "-o StrictHostKeyChecking=no", ";".join( ...
-
最简单的解决方案是将问题放在列表中,并使用全局变量来跟踪当前问题的索引。 “下一个问题”按钮需要简单地增加索引,然后显示下一个问题。 使用类将比全局变量更好,但要保持简短的例子,我不会使用类。 例: import Tkinter as tk current_question = 0 questions = [ "Shall we play a game?", "What's in the box?", "What is the airspeed velocity of an unl ...
-
在Linux中使用python同时执行多个命令(execute multiple commands in Linux using python in the same time)[2023-06-09]
看起来像是典型的生产者 - 消费者问题 import threading import os commands = ['ping www.google.com', 'ping www.yahoo.com', 'ping www.hotmail.com'] def worker_func(): while commands: # Checks if the the list is not-empty. Loop exits when list is becomes empty ...