English 中文(简体)
如何组成一个跨专业的JSON/Ajax网络服务
原标题:How to compose a Cross-Domain JSON/Ajax call to a simple web service
  • 时间:2011-11-08 17:48:02
  •  标签:
  • jquery

我有这样的网络服务:

http://<remote_domain_ip>/api/v1/search/?query=the_query

哪一份是JSON的回复文件。

我如何打造/满足一个名叫“JSON/ajax”的JQuery JSON/ajax呼声,以便我能够回头回来?

答复的形式如下:

{
"status_code":200,
"status_txt":"OK",
"field1":"abc",
"field2":"some_text"
}
最佳回答

它取决于。 如果在<条码>a.com<>上说的话,而且你的网络服务也在同一地点,那么你就能够这样做:

$.ajax( path-to-your-service/WebMethodName , data, function(r){
    // Success callback, you have your answer in r.d object
}, function(e){
    // Error callback
});

但是,如果你在<条码>b.com上,网络服务位于<条码>a.com,那么你受一个名为。 换言之,除非你遵守某些规则,否则你可以使用美国航天中心电话(或XMLHttpRequest)从该网络服务处取数据。 http://www.w3.org/TR/cors/“rel=“nofollow noreferer” HTTP Header field, which has some cross-browser issues. 另一种方式是使用JSONP,这要求服务器支持。

为了使用JSONP,你应当使用j Query s $.getJSON()方法,并提供一条警示,在通过HTTP GET寄出的参数之外,说明关键因素。

$.getJSON( path-to-your-service?x=some&y=thing?callback? , function(r){
    // Success callback. Your JSON object would be the r parameter
});

我建议,如果你是网络服务的创建者,如果你使用的话,请使用JSONP。 NET框架,我还建议你只使用Generic Handler,而不是一个网络服务,因为我们有许多问题组合网络服务。 但是,在你的服务器代码中,你应检查即将提出的GET申请,询问如何铺设钥匙叫ert

callback({
   a: a1,
   b: b1,
   c: c1
});

Understanding JSONP (the concept and the way it occurs) is very crucial. Without a deep understanding, your future maintenance would be a huge burden. I suggest reading this question.

问题回答

You can t, assuming is not the same domain you re on. If you have access to remote_domain_ip, or can get them to host a file for you, you can use EasyXDM. There are similar work-around tools involving Flash you can try. Otherwise, you ll need to perform the request on the back end.





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签