简体   繁体   English

Dojo2中的服务呼叫

[英]Service Call in Dojo2

I trying to get the data from server and trying to render in UI but while making service call I am getting the below error. 我试图从服务器获取数据并尝试在UI中呈现,但是在进行服务调用时出现以下错误。

Calling service API: 呼叫服务API:

const json = request('http://localhost:8080/part/findall').then(response => response.json());

Error: 错误:

Task.js:243 Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1
    at parse (<anonymous>)
    at Task.js:243
    at handler (ExtensiblePromise.js:137)

I am getting the data from server but while assign to json getting that above error. 我正在从服务器获取数据,但同时分配给json则得到上述错误。 Can you please help me where I am doing mistake. 您能在我做错的地方帮我吗?

My response Json: 我对杰森的回应:

[{"id":485,"orderno":"00605164","type":"typeA","description":"description"},{"id":486,"orderno":"00605164","type":"typeB","description":"description"}]

This sounds like a serverside issue and not dojo related. 这听起来像是服务器端问题,与dojo无关。

I reproduced your example with a sample json : 我用示例json复制了您的示例:

const json = request('https://jsonplaceholder.typicode.com/posts/1')
            .then(response => response.json());

and the data is fine. 并且数据很好。

Here is the complete example 这是完整的例子

import request from '@dojo/core/request';
import xhr from '@dojo/core/request/providers/xhr';

request.setDefaultProvider(xhr);
const json = request('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => response.json())
  .then(data => console.log(data));

Although the xhr is optional (it is the default, I guess) - this is fine in a browser and if you replace xhr by node it works fine in Node.js too. 尽管xhr是可选的(我猜它是默认值)-在浏览器中很好,如果用node替换xhr ,在Node.js中也可以正常工作。

How is your Server transferring the JSON - maybe as html ? 您的服务器如何传输JSON(也许是html)? Also: What exactly is your dojo version? 另外:您的dojo版本到底是什么?

More information for request than on https://dojo.io can be found in https://github.com/dojo/core/tree/master/src/request 可以在https://github.com/dojo/core/tree/master/src/request中找到比https://dojo.io上更多的请求信息。

您有一个数组作为响应,请尝试使用response [0] .json()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM