【问题标题】:Ajax cross-domain call to REST web service [duplicate]对 REST Web 服务的 Ajax 跨域调用 [重复]
【发布时间】:2012-11-29 20:22:16
【问题描述】:

可能重复:
Ways to circumvent the same-origin policy

我必须在 HTML 页面中使用 AJAX 调用从另一个 IP 调用 REST Web 服务。但是,AJAX 调用不支持跨域请求。

我正在使用 JSON-P,但在我的应用程序中没有得到结果。

这是我发出 JSON-P 请求的 jQuery 代码:

var makePUTRequest = function () {
            $.ajax({
                type: "POST",
                url: "http:// 
                contentType: "application/jsonp",
                data: '{"username1":"getStates", "password1":"EXPLORE"}',            
                dataType: "jsonp",
                success: function (response) {

                    if (response == ("success").toLocaleLowerCase()) {

                        alert("Loging Successfully!!..");
                        window.location = "patient_list.html";
                    }
                    else {
                        alert("Please Loging Again!!..");
                    }
                },
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("My-Key", 'MyKey123456789');
                },
                error: function (error) {

                    alert("ERROR:", error);

                },
                complete: function () {
                    alert("complete");
                }
            });

        };  

【问题讨论】:

  • 深呼吸,放慢速度,然后告诉我们您真正想要做什么。并在您使用时添加一些换行符。
  • 我想使用 ajax 请求在 html 页面中调用 rest web 服务。服务调用是跨域的,我的意思是我的 web 服务在其他域上,而我的 html 页面在不同的域上。
  • @Suhas:我们理解你在说什么,但你没有提供足够的信息让我们帮助你。 JSON-P 是执行跨域 AJAX 调用的正确方法。如果您尝试这样做,但它不起作用,那么您需要向我们展示您的代码并告诉我们运行它时会发生什么。

标签: jquery ajax jsonp


【解决方案1】:
     var makePUTRequest = function () {
                $.ajax({
                    type: "POST",
                    url: "http:// 
                    contentType: "application/jsonp",
                    data: '{"username1":"getStates", "password1":"EXPLORE"}',            
                    dataType: "jsonp",
                    success: function (response) {

                        if (response == ("success").toLocaleLowerCase()) {

                            alert("Loging Successfully!!..");
                            window.location = "patient_list.html";
                        }
                        else {
                            alert("Please Loging Again!!..");
                        }
                    },
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("My-Key", 'MyKey123456789');
                    },
                    error: function (error) {

                        alert("ERROR:", error);

                    },
                    complete: function () {
                        alert("complete");
                    }
                });

            };  

        </script>

js file 1.7.1.js download from google

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2013-12-07
    • 2012-01-30
    • 1970-01-01
    相关资源
    最近更新 更多