2012-05-10 33 views
0

我的大学成绩卡(结果)非常复杂,理解起来并且很难计算出学生的实际百分比。 因此,我想为学生提供一项服务,学生只需输入那里的入学号码即可。我会计算我自己的结果。jquery中的POST请求出错

非常首先,我试图发送POST请求等级卡页面中http://hurl.it 这里是烫发链接http://hurl.it/hurls/c61f1d38b6543965151a1c8a8d6f641b8921da49/986ecba51b61022fa9fa162be612d7c928051989

但是我得到错误时使用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; 
    } 
} 

更新1添加链接到网页 https://www.bobdn.com/IGNOU_BCA_Result.aspx

+3

你得到什么错误? – ocanal

+3

除非您在'error:function(a,b,c)'中得到一个JSON/JSONP对象作为响应 – Onheiron

+0

@ocanal,否则您无法执行跨域ajax。这只是一个错误。没有提供细节。 – shashwat

回答

1

这个错误是由于跨域策略,响应与正确的数据。但是本身是阻断响应浏览器回来,因为它是不相同的起源。

正如你可以在Twitter上https://twitter.com/crossdomain.xml看到这是跨域策略,则需要放置一个crossdomain.xml文件中的stusupport12.ignou.ac.in有类似内容的根:

<?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> 

我的asp.net样机:

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(); 
+0

我无权访问大学的服务器。我怎样才能做到这一点..??有没有其他的.. ??我可以在服务器端使用asp.net与C#处理此请求.. ????请帮忙 – shashwat

+0

在PHP中绕过跨域策略的方法是使用cURL包装器。通过发布后通话服务器端,您将绕过限制。 – Bloafer

+0

我不能在服务器端从asp.net进行post调用.. ?? – shashwat

0

我的猜测是错误不是在函数声明function (a, b, c),但在警报。我同意a, b, c是可怕的变量名 - 特别是b/c,a是XMLHttpRequest对象,而bc是字符串,这并不明显。
如果你意识到这一点,你会很容易看到错误 - 不能用字符串连接XMLHttpRequest对象。

查看错误的功能,这里的文档: http://api.jquery.com/jQuery.ajax/

+0

亲爱的,而使用HttpFox分析请求(在Firefox中添加)。我发现错误是'加载内容错误(NS_ERROR_DOCUMENT_NOT_CACHED)'。我仔细检查了'url'。网址中没有错误。你也可以看到。在hurl.it工作相同的网址 – shashwat

+0

我认为这个问题有你的答案在那里:http://stackoverflow.com/questions/4038299/why-does-this-javascript-work-on-safari-but-not-firefox as Onheiron说 - 不能跨域发布ajax没有JSONP – Stephen

+0

可能是错误是'text/html(NS_ERROR_DOM_BAD_URI)'我看到这个在httpfox某处 – shashwat

0

这里是点:

您参考网站(hurl.it)实际使用服务器端SCRIPTA使得其本身rver充当代理人并为您回顾结果。由于同源策略,我绝对肯定你不能执行这样的请求客户端。假设你可以自由地做这样的请求,你可以用未过滤的呼叫加载其他人的服务器,从而分配来自成千上万个不同IP的呼叫,而不是服务于你的服务。这就是为什么SOP要求您将自己的服务器置于其间,以便监控和指导这些请求。

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