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()联系服务器。我不关心这个反应。我的目的是记录一些用户操作的数据(他们点击的内容等)。当用户点击某些内容时,会调用$ .get,如下所示:
$.get(
"http://www.some-server.com/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.
如果我将数据类型更改为jsonp,我不会收到该错误但是当jquery回调尝试将日志服务器的响应评估为JSON时,它会告诉我“whatwasreturned”未定义。我无法在日志服务器上更改任何内容。
6 个解决方案
#1
6
Use a "tracking pixel" technique instead:
使用“跟踪像素”技术代替:
<img src="http://www.some-server.com/log.txt?click=1" height="1" width="1">
Just insert the HTML into the DOM when needed.
只需在需要时将HTML插入DOM即可。
#2
1
If you're using jquery, just add a script tag to avoid the same-origin-policy problem:
如果您正在使用jquery,只需添加脚本标记以避免同源策略问题:
$('body').append('<script src="http://www.some-server.com/log.txt?click=1"></script>');
#3
0
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.com"></script>
block and inserting it, means which means the remote server must reponse with valid JS code.
不确定你是否能够...如果你正在与你自己的服务器交谈,那么告诉JS将响应视为明文而不是尝试解码它是很简单的。但是一旦你正在做JSONP,那么jquery实际上只是构建一个
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.
另一种方法是加载包含json p url的图像。图像本身会被“破坏”,但仍然会触发GET请求。
#4
0
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(..)
因为你不能将ajax查询发送到其他域(然后脚本执行),因为你发现你可以使用jsonp,但只有当服务器支持它时(服务器基本上包含JSON)并且有$ .getJson(...的方法)。 )调用$ .ajax(..)更简单的方法
http://api.jquery.com/jQuery.getJSON/
example of server side (in php) that supports jsonp: http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
服务器端(在php中)支持jsonp的示例:http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
#5
0
You can keep using JSONP, but make your log.txt
return an empty JSON object: {}
.
您可以继续使用JSONP,但是让log.txt返回一个空的JSON对象:{}。
#6
0
You can use the pure ajax call:
你可以使用纯粹的ajax调用:
$.ajax({
url: 'http://www.some-server.com/log.txt?click=1',
dataType: 'jsonp',
crossDomain: true,
success: function(data) { /* do nothing */ }
});
#1
6
Use a "tracking pixel" technique instead:
使用“跟踪像素”技术代替:
<img src="http://www.some-server.com/log.txt?click=1" height="1" width="1">
Just insert the HTML into the DOM when needed.
只需在需要时将HTML插入DOM即可。
#2
1
If you're using jquery, just add a script tag to avoid the same-origin-policy problem:
如果您正在使用jquery,只需添加脚本标记以避免同源策略问题:
$('body').append('<script src="http://www.some-server.com/log.txt?click=1"></script>');
#3
0
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.com"></script>
block and inserting it, means which means the remote server must reponse with valid JS code.
不确定你是否能够...如果你正在与你自己的服务器交谈,那么告诉JS将响应视为明文而不是尝试解码它是很简单的。但是一旦你正在做JSONP,那么jquery实际上只是构建一个
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.
另一种方法是加载包含json p url的图像。图像本身会被“破坏”,但仍然会触发GET请求。
#4
0
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(..)
因为你不能将ajax查询发送到其他域(然后脚本执行),因为你发现你可以使用jsonp,但只有当服务器支持它时(服务器基本上包含JSON)并且有$ .getJson(...的方法)。 )调用$ .ajax(..)更简单的方法
http://api.jquery.com/jQuery.getJSON/
example of server side (in php) that supports jsonp: http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
服务器端(在php中)支持jsonp的示例:http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
#5
0
You can keep using JSONP, but make your log.txt
return an empty JSON object: {}
.
您可以继续使用JSONP,但是让log.txt返回一个空的JSON对象:{}。
#6
0
You can use the pure ajax call:
你可以使用纯粹的ajax调用:
$.ajax({
url: 'http://www.some-server.com/log.txt?click=1',
dataType: 'jsonp',
crossDomain: true,
success: function(data) { /* do nothing */ }
});