简体   繁体   English

JSON-RPC 不适用于 $http.POST

[英]JSON-RPC is not working with $http.POST

I am developing an ionic hybrid mobile app and trying to post data in JSON-RPC service but I am getting " 500 internal server error "我正在开发一个离子混合移动应用程序并尝试在 JSON-RPC 服务中发布数据,但我收到“ 500 内部服务器错误

        var req = {

        url: 'http://192.118.1.214:8080/remote/json-rpc',
        headers: {
            'content-type': 'application/json;charset=utf-8',
            'cache-control': 'no-cache, no-store, must-revalidate, max-age=0'
        },

        data: { jsonrpc: "2.0", id: 2, method: "HDAccess/getDevices", params: [null] }
    }


    $http(req).then(function (res) { alert(res); return res; },
                    function (er) { alert(er); });  

It is the request details from webconsole.这是来自 webconsole 的请求详细信息。 I have used chrome CORS plugin to avoid "CORS" issue我使用了 chrome CORS 插件来避免“CORS”问题

Request URL:http://192.118.1.214:8080/remote/json-rpc
Request Method:OPTIONS
Status Code:500 Internal Server Error
Remote Address:192.118.1.214:8080
Response Headers
view source
cache-control:no-cache, no-store, must-revalidate, max-age=0
content-length:810
content-type:text/html; charset=UTF-8
date:Tue, 01 Mar 2016 08:00:50 GMT
pragma:no-cache
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, cache-control, content-type
Access-Control-Request-Method:GET
Connection:keep-alive
Host:192.118.1.214:8080
Origin:http://evil.com/
Referer:http://localhost:8100/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36

I have used the same url and post method in POSTMAN extension and got worked but it is not worked in the above code.我在 POSTMAN 扩展中使用了相同的 url 和 post 方法并得到了工作,但它在上面的代码中不起作用。

Find the POSTMAN inputs below在下面找到 POSTMAN 输入

在此处输入图片说明

I have three doubts:我有三个疑问:

1.Why the data is not mentioned in the above request details. 1.为什么上述请求细节中没有提到数据。

2.Why the method is showed as "Option" but i am using "POST" method 2.为什么该方法显示为“选项”但我使用的是“POST”方法

3.why is it not working by code but working fine in the chrome POSTMAN extention client 3.为什么它不能通过代码工作,但在 chrome POSTMAN 扩展客户端中工作正常

Please guide me to solve this issue.请指导我解决这个问题。

Why the data is not mentioned in the above request details.为什么上述请求详细信息中未提及数据。

The data would be in the POST/PUT request that the browser would send if the server responded to the OPTIONS request with permission.如果服务器以许可响应 OPTIONS 请求,则数据将位于浏览器将发送的 POST/PUT 请求中。

Why the method is showed as "Option" but i am using "POST" method为什么该方法显示为“选项”但我使用的是“POST”方法

Since you are sending a request with a JSON formatted body, you are triggering a preflight OPTIONS request.由于您使用 JSON 格式的正文发送请求,因此您正在触发预检 OPTIONS 请求。

See also Why am I getting an OPTIONS request instead of a GET request?另请参阅为什么我收到 OPTIONS 请求而不是 GET 请求?

I have used chrome CORS plugin to avoid "CORS" issue我使用了 chrome CORS 插件来避免“CORS”问题

… it doesn't appear to be able to handle OPTIONS requests, at least not when the server responds with a 500 Internal Server Error . ...它似乎无法处理 OPTIONS 请求,至少在服务器响应500 Internal Server Error

Fix your server so it can respond to OPTIONS requests.修复您的服务器,使其能够响应 OPTIONS 请求。

why is it not working by code but working fine in the chrome POSTMAN extention client为什么它不能通过代码工作,但在 chrome POSTMAN 扩展客户端中工作正常

Because when you use Postman, there isn't a third party involved, so there are no trust issues to worry about.因为当您使用 Postman 时,没有涉及第三方,因此无需担心信任问题。 See also XMLHttpRequest cannot load https://www.[website].com/另请参阅XMLHttpRequest 无法加载 https://www.[website].com/

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

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