Ajax 进行API调用时出现BadArgument和JSON解析错误

Ajax 进行API调用时出现BadArgument和JSON解析错误,ajax,api,azure,react-native,fetch-api,Ajax,Api,Azure,React Native,Fetch Api,在我的组件中,我试图使用fetch进行api调用 API接收图像.jpg文件路径,例如-file:///var/mobile/Containers/Data/Application/1E1E919E-6C21-4CC7-B9C2-5B4B3BC84B0F/Library/Caches/ExponentExperienceData/%2540chuks93%252Fihu-main/Camera/F3B8EBCC-BB09-4603-AF7E-FD3CA792C237.jpg,它应该返回一个JSO

在我的组件中,我试图使用fetch进行api调用

API接收图像
.jpg
文件路径,例如-
file:///var/mobile/Containers/Data/Application/1E1E919E-6C21-4CC7-B9C2-5B4B3BC84B0F/Library/Caches/ExponentExperienceData/%2540chuks93%252Fihu-main/Camera/F3B8EBCC-BB09-4603-AF7E-FD3CA792C237.jpg
,它应该返回一个
JSON
对象

下面是我的取回电话:

export default  {

processImage: (image) => {
// Replace the subscriptionKey string value with your valid subscription key.
var subscriptionKey = "*******************";

var uriBase = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise";

// Display the image.
    var sourceImageUrl = image;
console.log(typeof sourceImageUrl)
console.log("here", sourceImageUrl);

// Perform the REST API call.
return fetch(uriBase, {

        method: 'POST',
    headers: {
        "Content-type": "application/json",
        "Ocp-Apim-Subscription-Key": subscriptionKey
    },

    body: JSON.stringify(sourceImageUrl),
    })
    .then((data) => data.json())
    .then(function (data){
        console.log("hello", data);
    })

    .catch(function (error) {
        console.log(error);
    });

  }
}
运行上述代码时,返回的错误如下:

对象{
“错误”:对象{
“代码”:“坏参数”,
“消息”:“JSON解析错误。”,
}

有什么想法吗?

改变你的想法

    body: JSON.stringify(sourceImageUrl),


使文章正文成为一个正确的json对象。

谢谢!!!它成功了!!!但现在我遇到了另一个问题:世博相机创建的照片文件返回了一个错误。
object{“error”:object{“code”:“InvalidURL”,“message”:“Invalid image URL.”,}
世博相机创建了这个:
“uri”:"file:///var/mobile/Containers/Data/Application/1E1E919E-6C21-4CC7-B9C2-5B4B3BC84B0F/Library/Caches/ExponentExperienceData/%2540chuks93%252Fihu-main/Camera/E67E8249-2B53-49EA-B51D-42CD98768634.jpg"
但是这会返回错误,但是来自任何大多数图像的任何图像地址都可以正常工作。我想你会发现工作的图像有如下的网络URLhttps://... 但该文件:///URL将不起作用…azure无法将文件本地加载到您的计算机。您需要将图像放在可以使用http://URL为其提供服务的位置。I ha我们甚至没有看过API,但也许有一种方法可以将图像作为有效负载的一部分而不仅仅是url内联。
    body: JSON.stringify({url: sourceImageUrl}),