Ajax跨域调用REST Web服务

时间:2012-12-12 07:04:28

标签: jquery ajax jsonp

  

可能重复:
  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");
                }
            });

        };  

1 个答案:

答案 0 :(得分:-2)

     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