Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery.get回调未从C#ASP.Net响应触发_C#_Asp.net_Jquery - Fatal编程技术网

jQuery.get回调未从C#ASP.Net响应触发

jQuery.get回调未从C#ASP.Net响应触发,c#,asp.net,jquery,C#,Asp.net,Jquery,我有一个书签,点击它会在当前页面(例如www.cnn.com)上打开一个div弹出窗口。此div有一个按钮,单击该按钮可向我的应用程序域发出jQuery.get请求。这一切都很好,但我得到了一个错误的回应。这是发出请求的函数 function postWishXML(){ jQuery(".ServerResponse").ajaxError(function(event, XMLHttpRequest, ajaxOptions){ //entering here });

我有一个书签,点击它会在当前页面(例如www.cnn.com)上打开一个div弹出窗口。此div有一个按钮,单击该按钮可向我的应用程序域发出jQuery.get请求。这一切都很好,但我得到了一个错误的回应。这是发出请求的函数

function postWishXML(){

  jQuery(".ServerResponse").ajaxError(function(event, XMLHttpRequest, ajaxOptions){
    //entering here
  });

  var Wish = wishObject();

  if(Wish == false)
  {
    jQuery('.ServerResponse').html('Please enter a gift title');
    jQuery('.GiftText').val('');
  }else
  {
    jQuery('.ServerResponse').html('');
    var WishXML = createXMLTags(Wish);

    jQuery.get(Root+'/apps/shop/toolbar/WishlistPopup.ashx',
       {'sap':Cookie,'wish':WishXML},
        Response,
       'text'
     );
  }
}

这是服务器代码

 public void ProcessRequest (HttpContext context) {


    if (SecurityContext.IsAuthenticated || SecurityContext.IsSemiAuthenticated)
    {

        if (!string.IsNullOrEmpty(context.Request["wish"]))
        {
            string res = "Your wish has been added";
            context.Response.ContentType = "text/plain";
            context.Response.Write(res);
            context.Response.End();

        }
    }
我的问题与跨域脚本或语法有关吗


谢谢,我想你是对的。看起来您遇到了跨站点脚本限制

记住,您正在将javascript代码插入目标网站的代码中。因此,这就像代码从他们的页面运行一样。换句话说,您正试图从页面(例如www.cnn.com)内发出AJAX请求,并从您的站点(另一个域)检索数据

我在一篇文章中找到了一个解决方案。要点如下:

function loadScript(scriptURL) {
 var scriptElem = document.createElement('SCRIPT');
 scriptElem.setAttribute('language', 'JavaScript');
 scriptElem.setAttribute('src', scriptURL);
 document.body.appendChild(scriptElem);
}

loadScript(Root + '/apps/shop/toolbar/WishlistPopup.ashx');
使服务器脚本返回将被评估的javascript

第二个,可能更可取的选择是使用。本质上,服务器将使用封装在命名函数中的JSON数据进行响应。告诉jQuery.ajax请求您希望得到jsonp响应以及返回函数的名称。这基本上就是在做上面的事情,但是有一点安全性和语法性

这将是PHP版本,但它应该能够理解这一点。假设您需要的所有数据都在$data中,请记住,您可以将匿名函数放在JSON结构中,以便在成功处理程序中进行评估

$json = json_encode($data);
echo $_GET['jsonp_callback'] . '(' . $json . ');';
javascript:

$.ajax({
  dataType: 'jsonp',
  jsonp: 'jsonp_callback',
  url: Root + '/apps/shop/toolbar/WishlistPopup.ashx',
  success: Response,
});

为了给予适当的信任,我从Andrew Moore的答案中盗取并修改了这个JSONP示例,该答案是关于

你在使用firebug吗?只需检查我正在使用firebug的响应,“事件”对象“类型”属性显示“ajaxError”,这并没有真正的帮助。我应该指出,用于处理回调的“响应”函数甚至没有被调用。函数是函数响应(数据){alert(数据);}James,谢谢。在阅读了您提供的JSONP链接之后,这听起来似乎会起作用。我还确认了这是一个跨域问题,因为应用程序在我的域上工作。