[英]jQuery Ajax POST to GET JSON and return value
Ok so I have a php
file that when a postcode has been posted
to it it then sends a get
request to retrieve some JSON
data. 好的,所以我有一个
php
文件,当邮政编码posted
到它时,它然后发送一个get
请求来检索一些JSON
数据。
The JSON
outputs like this: JSON
输出如下:
[{"AddressLine1":"West George Street","AddressLine2":null,"City":"Glasgow","County":"Lanarkshire","HouseName":"1","HouseNumber":"48","Postcode":"G21BP"},{"AddressLine1":"West George Street","AddressLine2":null,"City":"Glasgow","County":"Lanarkshire","HouseName":"3","HouseNumber":"48","Postcode":"G21BP"},{"AddressLine1":"West George Street","AddressLine2":null,"City":"Glasgow","County":"Lanarkshire","HouseName":"2","HouseNumber":"48","Postcode":"G21BP"},{"AddressLine1":"West George Street","AddressLine2":null,"City":"Glasgow","County":"Lanarkshire","HouseName":null,"HouseNumber":"46","Postcode":"G21BP"}]
And I'm using print_r($result);
我正在使用
print_r($result);
on the php
page to display the result 在
php
页面上显示结果
EDIT 编辑
The HTML of my output looks like so: 我的输出的HTML看起来像这样:
<html>
<head></head>
<body>
<pre>[{"AddressLine1":"Garelochhead","AddressLine2":null,"City":"Helensburgh","County":"Argyll And Bute","HouseName":null,"HouseNumber":"","Postcode":"G840EG"},{"AddressLine1":"Garelochhead","AddressLine2":null,"City":"Helensburgh","County":"Argyll And Bute","HouseName":"1","HouseNumber":"Flat 0","Postcode":"G840EG"},{"AddressLine1":"Garelochhead","AddressLine2":null,"City":"Helensburgh","County":"Argyll And Bute","HouseName":"1","HouseNumber":"Flat 1","Postcode":"G840EG"}]
</body>
</html>
My JS looks like bellow currently: 我的JS目前看起来像吼叫:
$('.btnFind').click(function() { var dataString; $.ajax({ url:baseUrl+'/postcode.php', type: 'post', datatype: "JSON", data: dataString, success: function() { //test } }).error (function() { alert('error with finding your address'); }).complete (function(data) { console.log(data); //var AddressLine1 = data[0].AddressLine1, // AddressLine2 = data[0].AddressLine2, // City = data[0].City, //County = data[0].County, // HouseName = data[0].HouseName, // HouseNumber = data[0].HouseNumber; alert(data[0].AddressLine1); }); });
but I keep getting the following error: 但我一直收到以下错误:
Uncaught TypeError: Cannot read property 'AddressLine1' of undefined
My guess is print_r isn't suitable but to be honest its been a while since I used JSON so I'm not up to scratch on it to be fair. 我的猜测是print_r不合适,但说实话它已经有一段时间了,因为我使用的是JSON,所以我不准备好对它公平。
Also its printing on aa balnk html page if that makes any difference too. 它也可以在一个balnk html页面打印,如果这也有所不同。
Console Log as requested 控制台日志按要求
Object
abort
:
(a)
always
:
()
complete
:
()
done
:
()
error
:
()
fail
:
()
getAllResponseHeaders
:
()
getResponseHeader
:
(a)
overrideMimeType
:
(a)
pipe
:
()
progress
:
()
promise
:
(a)
readyState
:
4
responseText
:
""
setRequestHeader
:
(a,b)
state
:
()
status
:
200
statusCode
:
(a)
statusText
:
"OK"
success
:
()
then
:
()
__proto__
:
Object
To return from your PHP to your JavaScript with a JSON use this: 要使用JSON从PHP返回到JavaScript,请使用以下命令:
header('Content-Type: application/json');
echo json_encode($data);
Edit: 编辑:
Also, make sure you give access to your PHP script, I use this: 另外,请确保您提供对PHP脚本的访问权限,我使用:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
Before referring to your object, it should be welcomed to the object. 在引用您的对象之前,应该欢迎该对象。 try this
尝试这个
data = JSON.parse(data);
alert(data[0].AddressLine1);
var parsed = JSON.parse(data);
var AddressLine1 = parsed[0].AddressLine1;
Thanks to everyone who posted and commented. 感谢所有发布和评论的人。
I just realised what daft mistake I made. 我刚刚意识到我犯了什么愚蠢的错误。 I never serialised the form first.
我从来没有先把表格序列化。
I had var dataString;
我有
var dataString;
when I should of had var dataString = $("form").serialize();
什么时候我应该有
var dataString = $("form").serialize();
total idiot I have wasted 4 hours of my day on something so damn stupid. 总白痴我浪费了4个小时的时间在这么该死的傻事上。
Thanks again to everyone for looking. 再次感谢大家的期待。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.