jquery JSON没有从jQ AJAX请求解析(jquery JSON not parsing from jQ AJAX request)
我对jQuery相当陌生,我的JSON数据似乎没有解析我期望的方式。
所以我用json_encode($ var)创建了我的数据; 在PHP中,这似乎工作正常:
{"id":"7","0":"7","ip":"86.152.117.159","1":"86.152.117.159","proxyip":"86.152.117.159","2":"86.152.117.159","ping":"1","3":"1","time":"03:39:17","4":"03:39:17"}{"id":"6","0":"6","ip":"86.152.117.159","1":"86.152.117.159","proxyip":"86.152.117.159","2":"86.152.117.159","ping":"1","3":"1","time":"03:36:20","4":"03:36:20"}
(这是通过jQuery的AJAX从同一服务器/域上的单独URL导入的)。
但是,当我使用此代码尝试导入它时:
$.ajax({url:"load.php",success:function(result){ var obj = jQuery.parseJSON(result); $(".response").html(obj.id); }});
并检查我的控制台,我得到“Uncaught SyntaxError:Unexpected token {”,它指向index.php中的“第1行” - 注意我没有使用外部JS文件。
我不知道我做错了什么以及如何解决这个问题所以我可以在页面上以非json形式显示数据(即在表格中)
任何帮助是极大的赞赏!
编辑:
这是我的load.php文件:
try { $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_db", $mysql_user, $mysql_pass); } catch(PDOException $e) { $e->getMessage(); } $update = "SELECT * FROM traffic"; foreach ($db->query($update) as $user) { echo json_encode($user); }
I'm fairly new to jQuery and my JSON data doesn't seem to be parsing the way i expect it to be.
So i've created my data by json_encode($var); in PHP and that seems to work fine:
{"id":"7","0":"7","ip":"86.152.117.159","1":"86.152.117.159","proxyip":"86.152.117.159","2":"86.152.117.159","ping":"1","3":"1","time":"03:39:17","4":"03:39:17"}{"id":"6","0":"6","ip":"86.152.117.159","1":"86.152.117.159","proxyip":"86.152.117.159","2":"86.152.117.159","ping":"1","3":"1","time":"03:36:20","4":"03:36:20"}
(This is being imported via jQuery's AJAX from a separate URL on the same-server/domain).
But when i use this code to try to import it:
$.ajax({url:"load.php",success:function(result){ var obj = jQuery.parseJSON(result); $(".response").html(obj.id); }});
and check my console, i get "Uncaught SyntaxError: Unexpected token {" which points to "line 1" in index.php - Note i'm not using an external JS file.
I don't know what i'm doing wrong and how i should go about fixing the issue so i can display the data in a non-json form on the page (i.e in a table)
Any help is greatly appreciated!
Edit:
Here's my load.php file:
try { $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_db", $mysql_user, $mysql_pass); } catch(PDOException $e) { $e->getMessage(); } $update = "SELECT * FROM traffic"; foreach ($db->query($update) as $user) { echo json_encode($user); }
原文:https://stackoverflow.com/questions/27660889
最满意答案
如果找到的部分始终是第二部分并且以“ - ”分隔,那么您可以使用以下公式:
=INDEX(F:F,MATCH(MID(A2,FIND("-",A2)+1,FIND("-",SUBSTITUTE(A2,MID(A2,FIND("-",A2)+1,999),""))-1),E:E,0))
如果没有,那么您将需要使用此数组公式:
=INDEX($F$2:$F$3,MATCH(TRUE,ISNUMBER(SEARCH($E$2:$E$3,A2)),0))
作为数组公式,需要在退出编辑模式时使用Ctrl-Shift-Enter而不是Enter来确认。 如果正确完成,Excel会在公式周围放置
{}
。If the part that is being found is always the second part and deliniated with "-" then you can use this formula:
=INDEX(F:F,MATCH(MID(A2,FIND("-",A2)+1,FIND("-",SUBSTITUTE(A2,MID(A2,FIND("-",A2)+1,999),""))-1),E:E,0))
If not then you will need to use this array formula:
=INDEX($F$2:$F$3,MATCH(TRUE,ISNUMBER(SEARCH($E$2:$E$3,A2)),0))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly Excel will put
{}
around the formula.
相关问答
更多-
匹配子字符串和字符串的匹配列表,并返回匹配的子字符串(Match list of substrings and strings and return substring if it matches)[2023-10-31]
解决这个问题的pandaic方法是根本不使用循环。 你可以用str.extract简单地做到这str.extract : p = '({})'.format('|'.join(subs)) df['SubMatch'] = df.Name.str.extract(p, expand=False).fillna('no match') df Name Number SubMatch 0 dogfood 1 dog 1 catfood ... -
在C2中输入数组公式 : =IFERROR(INDEX(B$2:B$8,SMALL((IF(A$2:A$8="Smith",ROW(INDIRECT("1:"&ROWS(A$2:A$8))))),ROWS($1:1)),1),"") 并复制下来: 必须使用Ctrl + Shift + Enter输入 数组公式 ,而不仅仅是Enter键。 In C2 enter the array formula: =IFERROR(INDEX(B$2:B$8,SMALL((IF(A$2:A$8="Smith",ROW(I ...
-
我不确定你的实际问题是什么。 要匹配“foo”或“bar”,你只需要"foo|bar"作为你的模式。 如果你想对字符串列表这样做,你可能想要单独检查每个字符串 - 你可以先加入字符串并检查它 ,但我不确定这会有多大用处。 如果你想获得与你的模式匹配的确切文本,你应该在括号中包围模式 - 例如"([fg]oo|[bt]ar)" ,这将匹配“foo”,“goo”,“bar “或”tar“ - 然后使用Match对象的Groups属性来检索这些捕获,这样您就可以确切地确定匹配的单词。 Groups[1]是第一个捕 ...
-
>>> import difflib >>> a = ["spam", "eggs", "spammy", "eggy"] >>> b = 'spam' >>> sorted(a, key=lambda x: difflib.SequenceMatcher(None, x, b).ratio()) ['eggy', 'eggs', 'spammy', 'spam'] 另外,如果你想要相反的顺序: >>> sorted(a, key=lambda x: difflib.SequenceMatcher(Non ...
-
而不是创建一个字符串并扩展它,你可以使用列表理解: matching_words = [x for x in all_strings if x in english_dict] 现在您可以使用", ".join(matching_sords)从该列表中创建一个字符串。 另一种选择 - 使用两组你可以使用&运算符: set(all_strings) & set(english_dict) 这里的结果将是一个包含两个列表中的项目的集合。 Instead of creating a string and ex ...
-
如果找到的部分始终是第二部分并且以“ - ”分隔,那么您可以使用以下公式: =INDEX(F:F,MATCH(MID(A2,FIND("-",A2)+1,FIND("-",SUBSTITUTE(A2,MID(A2,FIND("-",A2)+1,999),""))-1),E:E,0)) 如果没有,那么您将需要使用此数组公式: =INDEX($F$2:$F$3,MATCH(TRUE,ISNUMBER(SEARCH($E$2:$E$3,A2)),0)) 作为数组公式,需要在退出编辑模式时使用Ctrl-Shif ...
-
string val1 = (sample.Split(',').FirstOrDefault(w => myList.Any(m => w.Contains(m))) ?? string.Empty).Trim(); string val1 = (sample.Split(',').FirstOrDefault(w => myList.Any(m => w.Contains(m))) ?? string.Empty).Trim();
-
我需要完全更新答案: describe('testing', function() { var index = 'not found'; var text1 = []; it('should push element to array', function() { browser.ignoreSynchronization = true; browser.get('https://www.w3schools.com/angular/'); ...
-
如果匹配则返回匹配(return matches if match)[2023-09-27]
您可以使用next()从类似的生成器检索下一个匹配,默认值为False 。 请注意,这仅返回第一个匹配,显然不是每个匹配。 match = next((x for x in aListOfValues if x in line), False) 或者,一个非常简单的解决方案可能是将当前语句解构为循环并返回包含x和行的元组。 def find(line, aListOfValues): for x in aListOfValues: if x in line: ... -
查找匹配的字符串列表(Find list of strings that match)[2023-05-18]
我们能否安全地假设top3在您的数据集中是唯一的? 如果是这样: df <- read.table( textConnection(" V1 | V2 | V3 | top3 nyc| | | nyc, cool, nyc2016 cool| nyc | | nyc, cool, nyc2016 hello| cool | nyc | nyc, cool, nyc2016 winter| nyc | | nyc, cool, ...