从不同域调用处理程序并发送错误

时间:2013-11-15 12:46:36

标签: javascript jquery ajax

我正在从跨域调用ajax并获取Error ::

XMLHttpRequest无法加载http:// * ** :8080 / ActiveDirectiveActionRepulsionController.ashx。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许来源“http:// * * ”。

我的Ajax电话是

var url = http://********:8080/ActiveDirectiveActionRepulsionController.ashx
 $.ajax({
                                type: "POST",
                                url: url,
                                async:true,
                                data: { "REQUESTTYPE": 1, "USERNAME": forgotUsername.val() },
                                success: function (result) {
                                    if (result == "Username you provided  doesnot exist") {
                                        updateTips(result);
                                    }
                                    else if(result == "Sucessfull") {
                                        $(".validateTips").text("All form fields are required.");
                                        $("#Forgot-password").dialog("close");
                                        setDialogMessage("Your password has been send to your registered email id.");
                                        $("#dialog-forgot-message").dialog("open");
                                    }
                                },
                                error: function (jqXHR, exception) {
                                    if (jqXHR.status === 0) {
                                        alert('Not connect.\n Verify Network.');
                                    } else if (jqXHR.status == 404) {
                                        alert('Requested page not found. [404]');
                                    } else if (jqXHR.status == 500) {
                                        alert('Internal Server Error [500].');
                                    } else if (exception === 'parsererror') {
                                        alert('Requested JSON parse failed.');
                                    } else if (exception === 'timeout') {
                                        alert('Time out error.');
                                    } else if (exception === 'abort') {
                                        alert('Ajax request aborted.');
                                    } else {
                                        alert('Uncaught Error.\n' + jqXHR.responseText);
                                    }
                                }

                            });

var url = http://********:8080/ActiveDirectiveActionRepulsionController.ashx $.ajax({ type: "POST", url: url, async:true, data: { "REQUESTTYPE": 1, "USERNAME": forgotUsername.val() }, success: function (result) { if (result == "Username you provided doesnot exist") { updateTips(result); } else if(result == "Sucessfull") { $(".validateTips").text("All form fields are required."); $("#Forgot-password").dialog("close"); setDialogMessage("Your password has been send to your registered email id."); $("#dialog-forgot-message").dialog("open"); } }, error: function (jqXHR, exception) { if (jqXHR.status === 0) { alert('Not connect.\n Verify Network.'); } else if (jqXHR.status == 404) { alert('Requested page not found. [404]'); } else if (jqXHR.status == 500) { alert('Internal Server Error [500].'); } else if (exception === 'parsererror') { alert('Requested JSON parse failed.'); } else if (exception === 'timeout') { alert('Time out error.'); } else if (exception === 'abort') { alert('Ajax request aborted.'); } else { alert('Uncaught Error.\n' + jqXHR.responseText); } } });

1 个答案:

答案 0 :(得分:0)

当您向另一个域发送请求时,您必须在该域上设置CORS策略。

至少你必须设置Access-Control-Allow-Origin标题;一个完整的解决方案需要更多参与。

我经常会看到同样的问题,所以我有full answer on my blog