I can't seem to get my $ .ajax call to work correctly

Possible duplicate:
cross-origin 'Authorized header with jquery.ajax ()

JQuery

I am using http://code.jquery.com/jquery-1.7.2.min.js

If I type β†’ http: //pol638_047fe0/JSON.HTML? FN = GetPages & PIN = 7659 in the browser, I get a file with my json content

Javascript

var url = 'http://pol638_047fe0/JSON.HTML'; $.ajax({ url: url, type: 'GET', dataType: 'json', username: 'ADMIN', password: '1234', data: { 'FN': 'GetPages', 'PIN': '7659' }, xhrFields: { withCredentials: true }, sucess: function(data) { alert('done'); console.log('data', data); } }); 

Chrome Developer Tool

Console output: error with original error

 XMLHttpRequest cannot load http://pol638_047fe0/JSON.HTML?FN=GetPages&PIN=7659. Origin null is not allowed by Access-Control-Allow-Origin. 

This does not bother me, because the server doesn’t care who accesses the data with the correct username and password.

Network Header:

 Request URL:http://pol638_047fe0/JSON.HTML?FN=GetPages&PIN=7659 Request Headersview source Accept: '*/*' Cache-Control: max-age=0 Origin: null User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11 Query String Parametersview URL encoded FN: GetPages PIN: 7659 

Ok Something definitely went wrong here.

Now it’s strange if I add this ajax call to the code:

 ... $.ajax({ url : url, data: { 'FN' : 'GetPages', 'PIN' : '7659' } }); 

I get another JSON.HTML file on the network with the correct answer:

 Request URL: http://pol638_047fe0/JSON.HTML?FN=GetPages&PIN=7659 Request Method: GET Status Code: 200 OK Request Headersview source Accept: application/json, text/javascript, '*/*'; q=0.01 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Encoding: gzip,deflate,sdch Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4 Authorization: Basic QURNSU46U0JUQWRtaW4h Cache-Control: max-age=0 Connection: keep-alive Host: pol638_047fe0 Origin: null User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11 Query String Parametersview URL encoded FN: GetPages PIN: 7659 Response Headersview source Connection: close Content-Type: application/octet-stream Server: Keil-EWEB/2.1 

Answer:

 [{"pg":0,"descr":"PC1"},{"pg":1,"descr":"PC2"},{"pg":2,"descr":"PC3"},{"pg":3,"descr":"HG1"},{"pg":4,"descr":"HG2"},{"pg":5,"descr":"HG3"},{"pg":6,"descr":"HG4"},{"pg":7,"descr":"DW1"},{"pg":8,"descr":"DW2"},{"pg":9,"descr":"CMN"}] 

I have run out of ideas and I am grateful for any help or suggestions!

Trial

Changing the jsonp type helps me get the answer, but it doesn't help much, because I get Uncaught SyntaxError: Unexpected token ILLEGAL Error. I assume this is because the answer is not jsonp generated. Is there any way to get json response?

Change access headers on the server. I have no rights to this.

+4
source share
1 answer

I recommend you read this article: http://www.codeproject.com/Articles/42641/JSON-to-JSONP-Bypass-Same-Origin-Policy

XMLHttpRequest (ajax requests) does not allow cross-domain calls. This restriction is set by the browser itself. But it works great if you have a web service (or your web API) running in the same domain (the same as your website). Take a look at Same_origin_policy

0
source

Source: https://habr.com/ru/post/1447448/


All Articles