Jquery Ajax GET method header issue

247 views Asked by At

I'm trying to exchange data via http with a HUAWEI E3272 usb modem. With linux I had some success using curl:

curl -X GET "http://192.168.1.1/api/device/information" -H "__RequestVerificationToken: $(curl -s -X GET http://192.168.1.1/api/webserver/token | grep token  | cut -d '>' -f2 | cut -d '<' -f1)" -H "Content-Type: text/xml"

Getting the right response:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<DeviceName>E3272</DeviceName>
<SerialNumber>S7CBY14A17006941</SerialNumber>
<Imei>862600021252600</Imei>
<Imsi>724051120127678</Imsi>
<Iccid>89550536110015952771</Iccid>
<Msisdn></Msisdn>
<HardwareVersion>CH1E3272SM</HardwareVersion>
<SoftwareVersion>22.470.09.00.150</SoftwareVersion>
<WebUIVersion>13.100.11.00.150</WebUIVersion>
<MacAddress1>00:0D:87:8E:4B:AC</MacAddress1>
<MacAddress2></MacAddress2>
<ProductFamily>LTE</ProductFamily>
<Classify>hilink</Classify>
</response>

But I need to do it with jquery. I also tried 100 times, but I missing something that I dont know what it is. I need to pass a token via some header, before calling the method. I know that "g_requestVerificationToken" is getting values properly, because i debuged it.

This is my script:

var g_requestVerificationToken = '';
var headers = {};
  $.ajax({
  type: 'GET',
  url: "http://192.168.1.1/api/webserver/token",
  crossDomain: true,
  dataType: 'xml',
  success: function(data) {
  g_requestVerificationToken = (data).find("response").find("token").text();
  headers['__RequestVerificationToken'] = g_requestVerificationToken;
    $.ajax({
    type: 'GET',
    async: false,
    headers: headers,
    url: "http://192.168.1.1/api/device/information",
    crossDomain: true,
    ContentType: 'text/xml',
    success: function(data) {
    console.log(data);
    }});
  }});

Returning this error (beacause I couldn't pass the token session via Header):

<?xml version="1.0" encoding="UTF-8"?>
<error>
<code>125001</code>
<message></message>
</error>

And with it I'm getting these browse errors:

errors log

I'm using allow control allow origin plugin from Chrome Webstore to prevent some CORS errors.

The thing is: In linux via curl I'm able to properly communicate with the modem, but with jQuery not.

Can anyone give some help?

Thanks!!

1

There are 1 answers

0
jlaitio On

As far as I can see, your CORS setting makes jQuery do a pre-flight query via HTTP OPTIONS call (instead of a direct HTTP GET). Your server is telling that it has not implemented the OPTIONS method, hence returning HTTP 501 and jQuery throws in the towel at that point.

I'd look into if you can avoid the CORS hassle altogether - alternatively implement HTTP OPTIONS to return the appropriate CORS header on your server side.