jquery中的POST请求出错

jquery中的POST请求出错,jquery,asp.net,ajax,post,http-post,Jquery,Asp.net,Ajax,Post,Http Post,我的大学成绩卡(成绩)很难理解,很难计算学生的实际百分比。 所以,我想为学生提供一项服务,学生只需输入注册号。我会自己计算结果 首先,我试着向中的成绩卡页面发送一个post请求 这是烫发链接 但我在使用jquery从我的网站发送相同请求时遇到错误: 我使用以下代码发送请求 $.ajax({ type: "POST", url: "http://stusupport12.ignou.ac.in/Result.a

我的大学成绩卡(成绩)很难理解,很难计算学生的实际百分比。 所以,我想为学生提供一项服务,学生只需输入注册号。我会自己计算结果

首先,我试着向中的成绩卡页面发送一个post请求 这是烫发链接

但我在使用jquery从我的网站发送相同请求时遇到错误: 我使用以下代码发送请求

            $.ajax({
                type: "POST",
                url: "http://stusupport12.ignou.ac.in/Result.asp",
                data: "submit=Submit&eno=092853268&hidden%5Fsubmit=OK&Program=BCA",
                success: function (d) {
                    $("#resultDiv").html(d);
                },
                error: function (a, b, c) { alert(a + b + c); }
            });
请帮助我的朋友

更新2-我通过在服务器上处理请求来找到解决方案。我已经创建了一个通用处理程序(.ashx),它在服务器上处理请求,并将处理后的请求发送回客户端。我可以用Jquery来称呼它。

这是密码

<%@ WebHandler Language="C#" Class="IGNOU" %>

using System;
using System.Web;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net;
using System.IO;

public class IGNOU : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        context.Response.Write(Func(context.Request.QueryString["eno"]));
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
    public string Func(string eno)
    {
        string str = string.Empty;
        WebRequest request = WebRequest.Create("http://stusupport12.ignou.ac.in/Result.asp");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        string postData = "submit=Submit&Program=BCA&hidden_submit=OK&eno=" + eno;
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;
    }
}

使用制度;
使用System.Web;
使用Newtonsoft.Json;
使用System.Collections.Generic;
Net系统;
使用System.IO;
公共类IGNOU:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
context.Response.ContentType=“text/html”;
context.Response.Write(Func(context.Request.QueryString[“eno”]);
}
公共布尔可重用{
得到{
返回false;
}
}
公共字符串函数(字符串eno)
{
string str=string.Empty;
WebRequest=WebRequest.Create(“http://stusupport12.ignou.ac.in/Result.asp");
//将请求的Method属性设置为POST。
request.Method=“POST”;
//创建POST数据并将其转换为字节数组。
string postData=“submit=submit&Program=BCA&hidden\u submit=OK&eno=“+eno;
byte[]byteArray=System.Text.Encoding.UTF8.GetBytes(postData);
//设置WebRequest的ContentType属性。
request.ContentType=“application/x-www-form-urlencoded”;
//设置WebRequest的ContentLength属性。
request.ContentLength=byteArray.Length;
//获取请求流。
Stream dataStream=request.GetRequestStream();
//将数据写入请求流。
写入(byteArray,0,byteArray.Length);
//关闭流对象。
dataStream.Close();
//得到回应。
WebResponse=request.GetResponse();
//显示状态。
Console.WriteLine(((HttpWebResponse)response.StatusDescription);
//获取包含服务器返回的内容的流。
dataStream=response.GetResponseStream();
//使用StreamReader打开流以便于访问。
StreamReader=新的StreamReader(数据流);
//阅读内容。
字符串responseFromServer=reader.ReadToEnd();
//显示内容。
Console.WriteLine(responseFromServer);
//清理溪流。
reader.Close();
dataStream.Close();
response.Close();
返回responseFromServer;
}
}
更新1添加了网页链接
我猜错误不在函数声明
函数(a、b、c)
中,而是在警报中。我同意
a,b,c
是可怕的变量名-特别是b/c
a
是XMLHttpRequest对象并不明显,而
b
c
是字符串。
如果您意识到这一点,您将很容易看到错误-在XMLHttpRequest对象上显示一个字符串

请参见此处有关错误函数的文档:

此错误是由于跨域策略造成的,响应返回时包含正确的数据,但是浏览器本身阻止了响应,因为它不是同一来源

正如你在twitter上看到的
https://twitter.com/crossdomain.xml
这是跨域策略,您需要在
stupport12.ignou.ac.in
的根目录下放置一个
crossdomain.xml
文件,其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<cross-domain-policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFile.xsd">
  <allow-access-from domain="www.bobdn.com" />
</cross-domain-policy>
关键是:

您引用的站点(hurl.it)实际上使用服务器端脚本,使其服务器充当代理并为您检索结果。我绝对肯定,由于同源策略,您无法在客户端执行此类请求

想象一下,如果你可以自由地提出这样的请求,你就可以在别人的服务器上加载大量未经过滤的呼叫,从而分配来自数千个不同IP的呼叫,而这些呼叫并不涉及你的服务。这就是为什么SOP要求您将自己的服务器置于两者之间,以便监视和引导这些请求


另一个解决方案是远程服务器设置一个API服务来接收客户端调用,从而返回JSON响应,这将很好地工作。尽管所有API服务都需要一个额外的参数,该参数将是与您的域关联的个人开发者密钥,这样他们在任何情况下都可以知道这些请求来自哪里

除非您获得一个JSON/JSONP对象,否则无法执行跨域ajaxresponse@ocanal在
错误:函数(a、b、c)
。这只是一个错误。没有提供详细信息。为什么要使用
a
b
c
作为变量名。“那太糟糕了。”安海隆·赫尔。它能做到。为什么我不能。检查此链接我也尝试使用
错误:函数OnError(请求、状态、错误)
。我在使用HttpFox(firefox中的插件)分析请求时也遇到了同样的错误@RepWhoringPeeHaadear。我发现错误是加载内容时出错(NS\u error\u DOCUMENT\u NOT\u CACHED)。我仔细检查了
url
。url中没有错误。你也可以看到。hurl中的相同url.it working我想这个问题有你的答案:正如Onheiron所说的——没有JSONP就不能跨域发布ajax可能是错误是
text/html(NS\u error\u DOM\u BAD\u URI)
我在httpfox Somewhere中看到了相同基本问题的不同错误消息。请参阅:请参阅此处,了解使用
jsonp
request的示例:
byte[] buffer = Encoding.UTF8.GetBytes("submit=Submit&eno=092853268&hidden%5Fsubmit=OK&Program=BCA");

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://stusupport12.ignou.ac.in/Result.asp");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
req.CookieContainer = new CookieContainer(); // enable cookies 

Stream reqst = req.GetRequestStream(); // add form data to request stream
reqst.Write(buffer, 0, buffer.Length);
reqst.Flush();
reqst.Close();