简体   繁体   English

将cURL转换为JQuery.ajax请求

[英]Converting cURL to JQuery.ajax request

Hi I'm working on connecting to an API that is using Layer 7 as an IP authorizer and eGalaxy as a credentials authorizer, when the curl request is sent a line of xml is sent back to me. 嗨,我正在连接到使用第7层作为IP授权者和eGalaxy作为凭据授权者的API,当发送curl请求时,会向我发送xml行。 I'm currently working on localhost, I've implemented the Access-Control-Allow-Origin chrome extension . 我目前在localhost上工作,已经实现了Access-Control-Allow-Origin chrome 扩展

My curl request looks as such: 我的curl请求看起来像这样:

curl https://client-url/eGalaxy.aspx -H 'Content-Type:text/html' --data '<?xml version:"1.0" encoding="UTF-8"?><Envelope><Header><SourceID>0</SourceID><MessageID>131</MessageID><MessageType>Authenticate</MessageType></Header><Body><Authenticate><Username>*username*</Username><Password>*password*</Password><PasswordEncrypted>NO</PasswordEncrypted></Authenticate></Body></Envelope>' --insecure

When I tried to create an ajax request I receive an "Invalid HTTP status code 500" error and "OPTIONS url" which drops down to show: 当我尝试创建ajax请求时,我收到“无效的HTTP状态代码500”错误和“ OPTIONS url”(显示以下内容):

n.ajaxTransport.k.cors.a.crossDomain.send   @   jquery-2.1.3.js:4
n.extend.ajax   @   jquery-2.1.3.js:4
(anonymous function)    @   VM947:2
InjectedScript._evaluateOn  @   VM899:895
InjectedScript._evaluateAndWrap @   VM899:828
InjectedScript.evaluate @   VM899:694

My ajax code is as follows: 我的ajax代码如下:

$.ajax({
  url:'https://client-url/eGalaxy.aspx', 
  data:'<?xml version:"1.0" encoding="UTF-8"?><Envelope><Header>
        <SourceID>0</SourceID><MessageID>131</MessageID>
        <MessageType>Authenticate</MessageType></Header><Body>
        <Authenticate><Username>*username*</Username>
        <Password>*password*</Password>
        <PasswordEncrypted>NO</PasswordEncrypted></Authenticate></Body>
        </Envelope>', 
   type:'POST', 
   contentType:'text/xml', 
   dataType:'xml',
   success: function(data){
   },
   error: function(){
   }
 });

Any help with translating into a proper AJAX request would be appreciated! 任何帮助翻译成正确的AJAX请求均将不胜感激!

EDIT: If this makes a difference these are the headers that are returned with the client's xml when the curl is complete(client information deleted) 编辑:如果这有所作为,则这些是在卷毛完成时(客户端信息已删除)随客户端xml返回的标头 在此处输入图片说明

This application will be made into a widget as well, so it will not be running off of a hosting site. 该应用程序也将被制成小部件,因此不会在托管站点上运行。

UPDATE 1: I'm using @KevinB's suggestion that the CORS headers were still not properly added. 更新1:我使用@KevinB的建议,即仍未正确添加CORS标头。

Here is my updated JS code, copied from this link : 这是我更新的JS代码,从此链接复制而来:

var url = 'https://client-url/eGalaxy.aspx';
var data = '<?xml version="1.0" encoding="UTF-8"?><Envelope><Header><SourceID>1</SourceID><MessageID>131</MessageID><MessageType>Authenticate</MessageType></Header><Body><Authenticate><Username>*username*</Username><Password>*password</Password><PasswordEncrypted>NO</PasswordEncrypted></Authenticate></Body></Envelope>';
var xhr = createCORSRequest('POST', url);
xhr.send(data);
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('GET', url);
if (!xhr) {
throw new Error('CORS not supported');
}

When run with the CORS Chrome extension off I receive an Access-Control-Allow-Origin =! 在关闭CORS Chrome扩展程序的情况下运行时,我收到一个Access-Control-Allow-Origin =! 'null' error. 'null'错误。 Knowing that CORS needs Access-Control-Allow-Origin header to =! 知道CORS需要Access-Control-Allow-Origin标头才能=! 'null' will this cause problems in the future with making this into a widget that will be put into a Content Manager system? 如果将其制作为将被放入Content Manager系统的小部件,“空”会在将来引起问题吗?

With it on the origin is set to 'www.evil.com', with the only error in the code being that it says the xhr.send() is an anonymous method. 将其设置为“ www.evil.com”,代码中唯一的错误是它说xhr.send()是匿名方法。 Using breakpoints I can see the xhr in xhr.send() is set to an empty request: 使用断点,我可以看到xhr.send()中的xhr设置为空请求:

> XMLHttpRequest {response: "", responseText: ""}

Inside the createCORSRequest this line is undefined. 在createCORSRequest内部,此行是未定义的。 I've tested using 'GET' and 'POST' as the method. 我已经测试过使用“ GET”和“ POST”作为方法。

xhr.open(method, url, true)

EDIT 2: Using @Fabiano's approach I've changed the web.config for two versions of what I suspect is my server(?). 编辑2:使用@Fabiano的方法,我已经将web.config更改为我怀疑服务器(?)的两个版本。 I'm attaching screenshots of what I've gone through 我要附上我经历过的截图 我的系统的IIS管理器没有看到特定的网页,所以我选择了ASP Net页面更改版本2.0更改版本4.0 No luck, so far. 到目前为止没有运气。 Decided to use xhr.AppendHeader: 决定使用xhr.AppendHeader: 无法识别方法无法识别方法 I decided to use xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); 我决定使用xhr.setRequestHeader(“ Access-Control-Allow-Origin”,“ *”); xhr.RequestHeader The Network tab Headers for eGalaxy.aspx eGalaxy.aspx的“网络”选项卡标题 eGalaxy.aspx标头

There is an error in your XML. 您的XML错误。 You put version:"1.0" , and this makes the XML invalid. 您放置version:"1.0" ,这将使XML无效。 Change to version="1.0" and try to make your request. 更改为version="1.0"并尝试提出您的请求。 It should work. 它应该工作。 This may be the cause for the "Bad request" error. 这可能是导致“错误请求”错误的原因。

You can validate your XML here: enter link description here 您可以在此处验证XML:在此处输入链接描述

EDIT: After some research, the problem may be with the headers sent by your server. 编辑:经过一些研究,问题可能出在服务器发送的标头上。 Your server (or page, .aspx in this case) seems to skip the header you need, the "Access-Control-Allow-Origin: *". 您的服务器(在本例中为页面,即.aspx)似乎跳过了您需要的标题“ Access-Control-Allow-Origin:*”。

Look at this link: http://enable-cors.org/server.html This site shows you how to implement it for your server. 请看以下链接: http : //enable-cors.org/server.html该站点向您展示了如何为服务器实现它。 Since the page you are requesting is called eGalaxy.aspx, then you have 2 ways to implement the headers: 由于您请求的页面称为eGalaxy.aspx,因此您有2种方法实现标头:

1- Put the line Response.AppendHeader("Access-Control-Allow-Origin", "*"); 1-将行Response.AppendHeader("Access-Control-Allow-Origin", "*"); if the page is a simple ASP.NET application. 如果页面是一个简单的ASP.NET应用程序。 If it uses Web API 2, you need to implement a different way as it is shown here: http://enable-cors.org/server_aspnet.html 如果它使用Web API 2,则需要实现此处显示的其他方法: http : //enable-cors.org/server_aspnet.html

2- Edit the web.config file on the root of your server and add these lines inside the tag: 2-编辑服务器根目录上的web.config文件,并将以下行添加到标记中:

   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>

For a ASP.NET application, these are the ways you have. 对于ASP.NET应用程序,您可以采用以下方法。 The link I mentioned has solutions for other applications, take a look and choose the right one. 我提到的链接提供了其他应用程序的解决方案,请看一下并选择合适的解决方案。 :) :)

Note that the value * tells you that your server will accept any cross-origin request. 请注意,值*告诉您服务器将接受任何跨域请求。 This may lead to a security issue, so the best you can do is to put your domain address instead of *. 这可能会导致安全问题,因此,您最好的办法是输入您的域名地址而不是*。

I hope it helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM