简体   繁体   English

Ajax在不同的服务器上调用

[英]Ajax call on different server

I am sending ajax call on different server using script mentioned below. 我正在使用下面提到的脚本在不同的服务器上发送ajax call

$(document).ready(function() {
var uniqcod=$(".piczhu-widget").attr('id'); 

    $.ajax({
        url:'File Path...',
        type:'post',
        data:{uniId:uniqcod},
        success: function(result){
            $('.abcClass').html(result);
            }
        });
    });

Script is not receiving any response . 脚本没有收到任何回复 This script is working fine on the same server. 此脚本在同一台服务器上正常运行。 Is there any additional parameter to use to send call on different server? 是否有任何其他参数可用于在不同服务器上发送呼叫?

This should fix the issue using JSONP : 这应该使用JSONP解决问题:

$.ajax({
    url:'File Path...',
    type:'post',
    data:{uniId:uniqcod},
    dataType: 'jsonp', // use JSONP
    success: function(result){
        $('.abcClass').html(result);
        }
    });
});

This is because of cross-domain policy. 这是因为跨域策略。 It's a security thing. 这是安全的事情。 I recommend you to send that request to a PHP file with cURL that is located on your server (your domain). 我建议您将该请求发送到位于您的服务器(您的域)上的cURL的PHP​​文件。

But you need to have cURL installed on your server: http://curl.haxx.se/ If you're using Debian based server you can do it by: sudo apt-get install php5-curl 但是你需要在你的服务器上安装cURL: http//curl.haxx.se/如果你使用的是基于Debian的服务器,你可以通过以下方式完成:sudo apt-get install php5-curl

Example: 例:

<?php
$data = $_POST['data'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "URL FOR REQUEST");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                                                                                                                           

$result = curl_exec($ch);

echo $result;

?>

you need to use either jsonp or cors for cross domain ajax. 你需要使用jsonp或cors来跨域ajax。 The code given below is an example for cors 下面给出的代码是cors的一个例子

Example code: 示例代码:

jQuery.support.cors = true; 

function CrosDom_ajax(url) {
        if (window.XDomainRequest
        && $.browser.msie
        && $.browser.version < 10) {
        xdr = new XDomainRequest();
        if (xdr) {
            xdr.onload = function () {
               alert(xdr.responseText);

            };
            xdr.open("get", url);
            xdr.send();
        }
        }
        else {
            $.ajax({
                url: url,
                success: function (response) {


                },
                error: function (data) {
                }
            });
         }
    }

Also you need to Write the following code in server side, to allow cross domain access 您还需要在服务器端编写以下代码,以允许跨域访问

Response.AppendHeader("Access-Control-Allow-Origin", "*");           

Best and accepted method is to use JSONP to communicate with a different server. 最好的和接受的方法是使用JSONP与不同的服务器通信。 JSONP is a great away to get around cross-domain scripting errors. JSONP是一个很好的解决跨域脚本错误的方法。

Read the below links 阅读以下链接

What is JSONP all about? 什么是JSONP?

jsonp with jquery jsonp与jquery

What are the differences between JSON and JSONP? JSON和JSONP有什么区别?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM