PHP API JSON Response
PHP API JSON Response
我构建了一个非常简单的JSON响应来测试来自移动设备的AJAX请求。
我设置了一个本地域test.local并打印了一个json响应。
header("Content-Type:application/json; charset=utf-8");
echo json_encode(array('name' => 'Julio', 'city' => 'Miami'));
如果我使用浏览器访问http://test.local我得到:
{"name":"Julio","city":"Miami"}
但如果我使用AJAX请求访问,它会打印出"空"
$.ajax({
type: "GET",
url: "test.local",
dataType: "json",
success: function(data) {
alert(data.name + " " + data.city);
}
});
如果我将AJAX请求中的URL更改为http://jsonplaceholder.typicode.com/posts/1效果完美。
您拥有的URL是相对的,脚本试图访问/path/to/script/test.local
在开头添加/
或放置http://test.local
以使其绝对。
$.ajax({
type: "GET",
url: "http://test.local",
dataType: "json",
success: function(data) {
alert(data.name + " " + data.city);
}
});
适用于我。
相关文章:
- 以json形式获取post-response
- JSON response from API
- `同构的fetch`response.json()方法总是抛出意外的输入结束错误
- JSON and AJAX response
- response json to javascript
- 如何为此 Firebase Response JSON 构建打字稿界面
- Jquery AJAX response JSON in variable.
- jquery mobile login parse json response in jquery mobile
- Formatting response JSON data using AngularJS & ExpressJ
- django-ajax json response
- JSON.parse(hr.response) error
- express.js-如何拦截response.send()/response.json()
- double json response
- PHP API JSON Response
- Google API Response JSON
- 超链接在新的文档窗口中返回JSON.我希望它回到一个变量,比如response"
- Ruby on Rails, json vs js ajax response
- Rails + Ember + Ember Data empty JSON response
- 在Javascript中遍历JSON Response
- post json stringify response 403 Forbidden