Accessing API with $http POST Content-Type application/x-www-form-urlencoded

后端 未结 2 874
轻奢々
轻奢々 2020-12-11 11:56

I am trying to access this REST API, which accepts three parameters: stationId, crusherId, monthYear I am doing it like this in Angula

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 12:53

    The documentation says that the stationId and crusherId parameters should be arrays of strings. Also, it looks like you are sending JSON data, so make sure to set that header correctly.

    $http({
        headers: {
            'Content-Type': 'application/json', 
            'Accept':       'application/json'
        },
        url:    'https://fnrc.gov.ae/roayaservices/api/getHPData',
        method: 'POST',
        data: {
            stationId: ['263'], 
            crusherId: ['27'], 
            monthYear: '2016-4'
        }
    })
    

    When I change the code in your plunkr to use the corrected code above, I get the following response: "The requested resource does not support http method 'OPTIONS'."

    As the other (now deleted) answer correctly mentioned, this means that there is a CORS issue. The browser is trying to send a "preflight" request before making the cross-origin request, and the server doesn't know what to do with it. You can also see this message in the Chrome console:

    XMLHttpRequest cannot load https://fnrc.gov.ae/roayaservices/api/getHPData. Response for preflight has invalid HTTP status code 405

提交回复
热议问题