最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send GET Request to HTTP Server; don't care about the response - Stack Overflow

programmeradmin3浏览0评论

I'm trying to contact a server using $.get(). I don't care about the response. My purpose is to log some data about a user's action (what they clicked, etc). When the user clicks something, $.get is called like so:

$.get(
        ".txt?click=1", 

        function (data)
        {
        },

        "text"
);

The server handles that get request accordingly. I get the following error when the function executes:

XMLHttpRequest cannot load ... is not allowed by Access-Control-Allow-Origin.

If I change datatype to jsonp, I do not get that error but when the jquery callback tries to evaluate the log server's response as JSON, it tells me "whateverwasreturned" is not defined. I'm not able to change anything on the log server.

I'm trying to contact a server using $.get(). I don't care about the response. My purpose is to log some data about a user's action (what they clicked, etc). When the user clicks something, $.get is called like so:

$.get(
        "http://www.some-server./log.txt?click=1", 

        function (data)
        {
        },

        "text"
);

The server handles that get request accordingly. I get the following error when the function executes:

XMLHttpRequest cannot load ... is not allowed by Access-Control-Allow-Origin.

If I change datatype to jsonp, I do not get that error but when the jquery callback tries to evaluate the log server's response as JSON, it tells me "whateverwasreturned" is not defined. I'm not able to change anything on the log server.

Share Improve this question asked Nov 4, 2011 at 20:57 SemperFlySemperFly 1,5833 gold badges17 silver badges31 bronze badges 2
  • Does the server return any data? You said that you don't care about the response..its a bit confusing what your goal is here. – Sean Thoman Commented Nov 4, 2011 at 20:59
  • have you seen this question stackoverflow./questions/3595515/… – Nathan Commented Nov 4, 2011 at 21:01
Add a ment  | 

6 Answers 6

Reset to default 6

Use a "tracking pixel" technique instead:

<img src="http://www.some-server./log.txt?click=1" height="1" width="1">

Just insert the HTML into the DOM when needed.

If you're using jquery, just add a script tag to avoid the same-origin-policy problem:

$('body').append('<script src="http://www.some-server./log.txt?click=1"></script>');

Not sure if you'll be able to... if you were talking to your own server, then it'd be simple to tell JS to treat the response as plaintext and not try to decode it. But once you're doing JSONP, then jquery is really just building a <script src="http://otherserver."></script> block and inserting it, means which means the remote server must reponse with valid JS code.

One alternative would be to load an image containing your json p url. The image itself would be "broken", but would still trigger a GET request.

Becouse of same origin policy you cannot send ajax query to other domain (then script executes) but as you discover you can use jsonp but only when server support it (server basicly waraps JSON) and there is method for that $.getJson(..) it's easier way to call $.ajax(..)

http://api.jquery./jQuery.getJSON/

example of server side (in php) that supports jsonp: http://www.geekality/2010/06/27/php-how-to-easily-provide-json-and-jsonp/

You can keep using JSONP, but make your log.txt return an empty JSON object: {}.

You can use the pure ajax call:

$.ajax({
  url: 'http://www.some-server./log.txt?click=1',
  dataType: 'jsonp',
  crossDomain: true,
  success: function(data) { /* do nothing */ }
});
发布评论

评论列表(0)

  1. 暂无评论